/** * Checking if we can sale this bundle * * @param \Magento\Catalog\Model\Product $product * @return bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function isSalable($product) { if (!parent::isSalable($product)) { return false; } if ($product->hasData('all_items_salable')) { return $product->getData('all_items_salable'); } $optionCollection = $this->getOptionsCollection($product); if (!count($optionCollection->getItems())) { return false; } $requiredOptionIds = []; foreach ($optionCollection->getItems() as $option) { if ($option->getRequired()) { $requiredOptionIds[$option->getId()] = 0; } } $selectionCollection = $this->getSelectionsCollection($optionCollection->getAllIds(), $product); if (!count($selectionCollection->getItems())) { return false; } $salableSelectionCount = 0; foreach ($selectionCollection as $selection) { /* @var $selection \Magento\Catalog\Model\Product */ if ($selection->isSalable()) { $selectionEnoughQty = $this->_stockRegistry->getStockItem($selection->getId())->getManageStock() ? $selection->getSelectionQty() <= $this->_stockState->getStockQty($selection->getId()) : $selection->isInStock(); if (!$selection->hasSelectionQty() || $selection->getSelectionCanChangeQty() || $selectionEnoughQty) { $requiredOptionIds[$selection->getOptionId()] = 1; $salableSelectionCount++; } } } $isSalable = array_sum($requiredOptionIds) == count($requiredOptionIds) && $salableSelectionCount; $product->setData('all_items_salable', $isSalable); return $isSalable; }
/** * Retrieve product stock qty * * @param Product $product * @return float */ public function getProductStockQty($product) { return $this->stockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId()); }
public function testGetStockQty() { $this->assertEquals($this->qty, $this->stockState->getStockQty($this->productId, $this->websiteId)); }