/** * Get Stock Data * * @param StockItemInterface $stockItem * @return array */ private function getData(StockItemInterface $stockItem) { $result = $stockItem->getData(); $result[StockItemInterface::MANAGE_STOCK] = (int) $stockItem->getManageStock(); $result[StockItemInterface::QTY] = (double) $stockItem->getQty(); $result[StockItemInterface::MIN_QTY] = (double) $stockItem->getMinQty(); $result[StockItemInterface::MIN_SALE_QTY] = (double) $stockItem->getMinSaleQty(); $result[StockItemInterface::MAX_SALE_QTY] = (double) $stockItem->getMaxSaleQty(); $result[StockItemInterface::IS_QTY_DECIMAL] = (int) $stockItem->getIsQtyDecimal(); $result[StockItemInterface::IS_DECIMAL_DIVIDED] = (int) $stockItem->getIsDecimalDivided(); $result[StockItemInterface::BACKORDERS] = (int) $stockItem->getBackorders(); $result[StockItemInterface::NOTIFY_STOCK_QTY] = (double) $stockItem->getNotifyStockQty(); $result[StockItemInterface::ENABLE_QTY_INCREMENTS] = (int) $stockItem->getEnableQtyIncrements(); $result[StockItemInterface::QTY_INCREMENTS] = (double) $stockItem->getQtyIncrements(); $result[StockItemInterface::IS_IN_STOCK] = (int) $stockItem->getIsInStock(); return $result; }
/** * Retrieve stock qty whether product is composite or no * * @param StockItemInterface $stockItem * @return float * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function getStockQty(StockItemInterface $stockItem) { if (!$stockItem->hasStockQty()) { $stockItem->setStockQty(0); $product = $this->productFactory->create(); $product->load($stockItem->getProductId()); // prevent possible recursive loop if (!$product->isComposite()) { $stockQty = $stockItem->getQty(); } else { $stockQty = null; $productsByGroups = $product->getTypeInstance()->getProductsToPurchaseByReqGroups($product); foreach ($productsByGroups as $productsInGroup) { $qty = 0; foreach ($productsInGroup as $childProduct) { $qty += $this->getStockQty($stockItem); } if (null === $stockQty || $qty < $stockQty) { $stockQty = $qty; } } } $stockQty = (double) $stockQty; if ($stockQty < 0 || !$stockItem->getManageStock() || !$stockItem->getIsInStock() || !$product->isSaleable()) { $stockQty = 0; } $stockItem->setStockQty($stockQty); } return (double) $stockItem->getData('stock_qty'); }