|
5 | 5 | */ |
6 | 6 | namespace Magento\CatalogInventory\Model\ResourceModel\Stock; |
7 | 7 |
|
8 | | -use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 8 | +use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexProcessor; |
9 | 9 | use Magento\CatalogInventory\Api\StockConfigurationInterface; |
10 | 10 | use Magento\CatalogInventory\Model\Stock; |
11 | 11 | use Magento\CatalogInventory\Model\Indexer\Stock\Processor; |
12 | 12 | use Magento\Framework\Model\AbstractModel; |
13 | 13 | use Magento\Framework\Model\ResourceModel\Db\Context; |
14 | 14 | use Magento\Framework\DB\Select; |
15 | | -use Magento\Framework\App\ObjectManager; |
16 | 15 | use Magento\Framework\Stdlib\DateTime\DateTime; |
17 | 16 |
|
18 | 17 | /** |
@@ -42,27 +41,33 @@ class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb |
42 | 41 | */ |
43 | 42 | private $dateTime; |
44 | 43 |
|
| 44 | + /** |
| 45 | + * @var PriceIndexProcessor |
| 46 | + */ |
| 47 | + private $priceIndexProcessor; |
| 48 | + |
45 | 49 | /** |
46 | 50 | * @param Context $context |
47 | 51 | * @param Processor $processor |
48 | | - * @param string $connectionName |
49 | 52 | * @param StockConfigurationInterface $stockConfiguration |
50 | 53 | * @param DateTime $dateTime |
| 54 | + * @param PriceIndexProcessor $priceIndexProcessor |
| 55 | + * @param string $connectionName |
51 | 56 | */ |
52 | 57 | public function __construct( |
53 | 58 | Context $context, |
54 | 59 | Processor $processor, |
55 | | - $connectionName = null, |
56 | | - StockConfigurationInterface $stockConfiguration = null, |
57 | | - DateTime $dateTime = null |
| 60 | + StockConfigurationInterface $stockConfiguration, |
| 61 | + DateTime $dateTime, |
| 62 | + PriceIndexProcessor $priceIndexProcessor, |
| 63 | + $connectionName = null |
58 | 64 | ) { |
59 | 65 | $this->stockIndexerProcessor = $processor; |
60 | 66 | parent::__construct($context, $connectionName); |
61 | 67 |
|
62 | | - $this->stockConfiguration = $stockConfiguration ?? |
63 | | - ObjectManager::getInstance()->get(StockConfigurationInterface::class); |
64 | | - $this->dateTime = $dateTime ?? |
65 | | - ObjectManager::getInstance()->get(DateTime::class); |
| 68 | + $this->stockConfiguration = $stockConfiguration; |
| 69 | + $this->dateTime = $dateTime; |
| 70 | + $this->priceIndexProcessor = $priceIndexProcessor; |
66 | 71 | } |
67 | 72 |
|
68 | 73 | /** |
@@ -144,10 +149,28 @@ protected function _prepareDataForTable(\Magento\Framework\DataObject $object, $ |
144 | 149 | protected function _afterSave(AbstractModel $object) |
145 | 150 | { |
146 | 151 | parent::_afterSave($object); |
147 | | - /** @var StockItemInterface $object */ |
| 152 | + |
| 153 | + $productId = $object->getProductId(); |
148 | 154 | if ($this->processIndexEvents) { |
149 | | - $this->stockIndexerProcessor->reindexRow($object->getProductId()); |
| 155 | + $this->addCommitCallback(function () use ($productId) { |
| 156 | + $this->stockIndexerProcessor->reindexRow($productId); |
| 157 | + }); |
| 158 | + |
150 | 159 | } |
| 160 | + $fields = [ |
| 161 | + 'is_in_stock', |
| 162 | + 'use_config_manage_stock', |
| 163 | + 'manage_stock', |
| 164 | + ]; |
| 165 | + foreach ($fields as $field) { |
| 166 | + if ($object->dataHasChangedFor($field)) { |
| 167 | + $this->addCommitCallback(function () use ($productId) { |
| 168 | + $this->priceIndexProcessor->reindexRow($productId); |
| 169 | + }); |
| 170 | + break; |
| 171 | + } |
| 172 | + } |
| 173 | + |
151 | 174 | return $this; |
152 | 175 | } |
153 | 176 |
|
|
0 commit comments