Example #1
0
 /**
  * Reindex CatalogInventory save event
  *
  * @param AbstractModel $object
  * @return $this
  */
 protected function _afterSave(AbstractModel $object)
 {
     parent::_afterSave($object);
     /** @var StockItemInterface $object */
     if ($this->processIndexEvents) {
         $this->stockIndexerProcessor->reindexRow($object->getProductId());
     }
     return $this;
 }
 /**
  * @inheritdoc
  */
 public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
 {
     try {
         /** @var \Magento\Catalog\Model\Product $product */
         $product = $this->productFactory->create();
         $product->load($stockItem->getProductId());
         if (!$product->getId()) {
             return $stockItem;
         }
         $typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
         $isQty = $this->stockConfiguration->isQty($typeId);
         if ($isQty) {
             $isInStock = $this->stockStateProvider->verifyStock($stockItem);
             if ($stockItem->getManageStock() && !$isInStock) {
                 $stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
             }
             // if qty is below notify qty, update the low stock date to today date otherwise set null
             $stockItem->setLowStockDate(null);
             if ($this->stockStateProvider->verifyNotification($stockItem)) {
                 $stockItem->setLowStockDate((new \DateTime())->format('Y-m-d H:i:s'));
             }
             $stockItem->setStockStatusChangedAuto(0);
             if ($stockItem->hasStockStatusChangedAutomaticallyFlag()) {
                 $stockItem->setStockStatusChangedAuto((int) $stockItem->getStockStatusChangedAutomaticallyFlag());
             }
         } else {
             $stockItem->setQty(0);
         }
         $stockItem->setWebsiteId($stockItem->getWebsiteId());
         $stockItem->setStockId($stockItem->getStockId());
         $this->resource->save($stockItem);
         $this->indexProcessor->reindexRow($stockItem->getProductId());
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__($exception->getMessage()));
     }
     return $stockItem;
 }
Example #3
0
 /**
  * Reindex CatalogInventory save event
  *
  * @return $this
  */
 protected function _afterSave()
 {
     parent::_afterSave();
     if ($this->_processIndexEvents) {
         $this->_stockIndexerProcessor->reindexRow($this->getProductId());
     }
     return $this;
 }