/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php */ public function testIsSalable() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $this->assertTrue($this->_model->isSalable($product)); $product->loadByAttribute('sku', 'simple'); // fixture $this->assertTrue((bool) $this->_model->isSalable($product)); }
/** * Check is product available for sale * * @param \Magento\Catalog\Model\Product $product * @return bool */ public function isSalable($product) { $salable = parent::isSalable($product); if ($salable !== false) { $salable = false; if (!is_null($product)) { $this->setStoreFilter($product->getStoreId(), $product); } /** @var \Magento\Catalog\Model\Product $child */ foreach ($this->getUsedProducts($product) as $child) { if ($child->isSalable()) { $salable = true; break; } } } return $salable; }
/** * 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; }
/** * Checking if we can sale this bundle * * @param \Magento\Catalog\Model\Product $product * @return bool */ public function isSalable($product) { $salable = parent::isSalable($product); if (!is_null($salable)) { return $salable; } $optionCollection = $this->getOptionsCollection($product); if (!count($optionCollection->getItems())) { return false; } $requiredOptionIds = array(); 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) { if ($selection->isSalable()) { $requiredOptionIds[$selection->getOptionId()] = 1; $salableSelectionCount++; } } return array_sum($requiredOptionIds) == count($requiredOptionIds) && $salableSelectionCount; }