public function testIsQty() { $configAll = [1 => ['is_qty' => true], 2 => ['is_qty' => false], 3 => []]; $this->config->expects($this->once())->method('getAll')->will($this->returnValue($configAll)); $this->assertTrue($this->model->isQty(1)); $this->assertFalse($this->model->isQty(2)); $this->assertFalse($this->model->isQty(3)); $this->assertFalse($this->model->isQty(4)); }
/** * Get back to stock (when order is canceled or whatever else) * * @param int $productId * @param int|float $qty * @return $this */ public function backItemQty($productId, $qty) { /** @var Item $stockItem */ $stockItem = $this->_stockItemFactory->create()->loadByProduct($productId); if ($stockItem->getId() && $this->stockItemService->isQty($this->getProductType($productId))) { $stockItem->addQty($qty); if ($stockItem->getCanBackInStock() && $stockItem->getQty() > $stockItem->getMinQty()) { $stockItem->setIsInStock(true)->setStockStatusChangedAutomaticallyFlag(true); } $stockItem->save(); } return $this; }
/** * Stock item saving. * * @return $this */ protected function _saveStockItem() { /** @var $stockResource \Magento\CatalogInventory\Model\Resource\Stock\Item */ $stockResource = $this->_stockResItemFac->create(); $entityTable = $stockResource->getMainTable(); while ($bunch = $this->_dataSourceModel->getNextBunch()) { $stockData = array(); $productIdsToReindex = array(); // Format bunch to stock data rows foreach ($bunch as $rowNum => $rowData) { if (!$this->isRowAllowedToImport($rowData, $rowNum)) { continue; } // only SCOPE_DEFAULT can contain stock data if (self::SCOPE_DEFAULT != $this->getRowScope($rowData)) { continue; } $row = array(); $row['product_id'] = $this->_newSku[$rowData[self::COL_SKU]]['entity_id']; $productIdsToReindex[] = $row['product_id']; $row['stock_id'] = \Magento\CatalogInventory\Model\Stock\Item::DEFAULT_STOCK_ID; $stockItemDo = $this->stockItemService->getStockItem($row['product_id']); $existStockData = $stockItemDo->__toArray(); $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row); $row = $this->stockItemService->processIsInStock($row); if ($this->stockItemService->isQty($this->_newSku[$rowData[self::COL_SKU]]['type_id'])) { if ($this->stockItemService->verifyNotification($row['product_id'])) { $row['low_stock_date'] = $this->_localeDate->date(null, null, null, false)->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); } $row['stock_status_changed_auto'] = (int) (!$this->stockItemService->verifyStock($row['product_id'])); } else { $row['qty'] = 0; } $stockData[] = $row; } // Insert rows if (!empty($stockData)) { $this->_connection->insertOnDuplicate($entityTable, $stockData); } if ($productIdsToReindex) { $this->newIndexer->load('catalog_product_category')->reindexList($productIdsToReindex); } } return $this; }
/** * Before save prepare process * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productFactory->create(); $product->load($this->getProductId()); $typeId = $product->getTypeId() ? $product->getTypeId() : $this->getTypeId(); $isQty = $this->stockItemService->isQty($typeId); if ($isQty) { if (!$this->getId()) { $this->processIsInStock(); } if ($this->getManageStock() && !$this->verifyStock()) { $this->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true); } // if qty is below notify qty, update the low stock date to today date otherwise set null $this->setLowStockDate(null); if ($this->verifyNotification()) { $this->setLowStockDate($this->_localeDate->date(null, null, null, false)->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT)); } $this->setStockStatusChangedAuto(0); if ($this->hasStockStatusChangedAutomaticallyFlag()) { $this->setStockStatusChangedAuto((int) $this->getStockStatusChangedAutomaticallyFlag()); } } else { $this->setQty(0); } return $this; }