Skip to content

Commit aa0c3d3

Browse files
authored
Merge pull request #5 from zcuric/master
Fix integrity constraint violation issue on reindex
2 parents 175eddb + c035c96 commit aa0c3d3

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

Model/SimpleProductsAggregatedReportDataProcessor.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,45 @@ class SimpleProductsAggregatedReportDataProcessor
4444
*/
4545
private $collection;
4646

47+
protected $_productCollectionFactory;
48+
4749
public function __construct(
4850
\MagentoHackathon\BestsellersSorting\Model\ResourceModel\Bestseller $bestseller,
4951
\Magento\Catalog\Model\ResourceModel\Product\Action $action,
50-
\MagentoHackathon\BestsellersSorting\Model\ResourceModel\Bestseller\Collection $collection
51-
52+
\MagentoHackathon\BestsellersSorting\Model\ResourceModel\Bestseller\Collection $collection,
53+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
5254
)
5355
{
5456

5557
$this->bestseller = $bestseller;
5658
$this->action = $action;
5759
$this->collection = $collection;
60+
$this->_productCollectionFactory = $productCollectionFactory;
5861
}
5962

6063
public function calculate($storeId = 0)
6164
{
6265
$this->bestseller->aggregate(null, null);
63-
66+
67+
$productIds = $this->_productCollectionFactory->create()->getAllIds();
68+
6469
$collection = $this->collection;
6570
/** @var \MagentoHackathon\BestsellersSorting\Model\Bestseller $productId */
6671
foreach ($collection as $item) {
6772

73+
/**
74+
* https://github.com/magento-hackathon/bestsellers-sorting-m2/issues/1
75+
* Integrity constraint violation issue on running reindex #1
76+
*/
77+
if(!in_array($item->getProductId(),$productIds)) {
78+
continue;
79+
}
80+
6881
/**https://magento.stackexchange.com/questions/151186/best-way-to-update-products-attribute-value */
6982
$this->action->updateAttributes(
7083
[$item->getProductId()],
7184
['bestseller_order' => $item->getRatingPos()],
7285
$item->getStoreId());
7386
}
74-
75-
7687
}
7788
}

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
2-
"name": "magento-hackathon/module-bestsellers-sorting",
2+
"name": "zcuric/module-bestsellers-sorting",
3+
"description": "Bestsellers sorting of products in categories for Magento 2.",
34
"license": "GPL-3.0",
45
"type": "magento2-module",
5-
"description": "Bestsellers sorting of products in categories for Magento 2.",
6+
"version": "0.1.0",
67
"require": {
8+
"php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0|~7.2.0|~7.3.0|~7.4.0"
79
},
810
"authors":[
911
{

0 commit comments

Comments
 (0)