Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexQuoteInventoryObserver.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
18 
22  protected $priceIndexer;
23 
27  protected $itemsForReindex;
28 
34  public function __construct(
35  \Magento\CatalogInventory\Model\Indexer\Stock\Processor $stockIndexerProcessor,
36  \Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer,
38  ) {
39  $this->stockIndexerProcessor = $stockIndexerProcessor;
40  $this->priceIndexer = $priceIndexer;
41  $this->itemsForReindex = $itemsForReindex;
42  }
43 
50  public function execute(EventObserver $observer)
51  {
52  // Reindex quote ids
53  $quote = $observer->getEvent()->getQuote();
54  $productIds = [];
55  foreach ($quote->getAllItems() as $item) {
56  $productIds[$item->getProductId()] = $item->getProductId();
57  $children = $item->getChildrenItems();
58  if ($children) {
59  foreach ($children as $childItem) {
60  $productIds[$childItem->getProductId()] = $childItem->getProductId();
61  }
62  }
63  }
64 
65  if ($productIds) {
66  $this->stockIndexerProcessor->reindexList($productIds);
67  }
68 
69  // Reindex previously remembered items
70  $productIds = [];
71  foreach ($this->itemsForReindex->getItems() as $item) {
72  $item->save();
73  $productIds[] = $item->getProductId();
74  }
75 
76  if (!empty($productIds)) {
77  $this->priceIndexer->reindexList($productIds);
78  }
79 
80  $this->itemsForReindex->clear();
81  // Clear list of remembered items - we don't need it anymore
82  }
83 }
$quote
__construct(\Magento\CatalogInventory\Model\Indexer\Stock\Processor $stockIndexerProcessor, \Magento\Catalog\Model\Indexer\Product\Price\Processor $priceIndexer, ItemsForReindex $itemsForReindex)
$children
Definition: actions.phtml:11