/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testCheckProductBuyStateException() { $repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\ProductRepository'); $product = $repository->get('simple'); // fixture $this->_model->checkProductBuyState($product); }
/** * @magentoDataFixture Magento/Catalog/_files/product_simple.php * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testCheckProductBuyStateException() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); // fixture $this->_model->checkProductBuyState($product); }
/** * Check if product can be bought * * @param \Magento\Catalog\Model\Product $product * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function checkProductBuyState($product) { parent::checkProductBuyState($product); $option = $product->getCustomOption('info_buyRequest'); if ($option instanceof \Magento\Quote\Model\Quote\Item\Option) { $buyRequest = new \Magento\Framework\Object(unserialize($option->getValue())); $attributes = $buyRequest->getSuperAttribute(); if (is_array($attributes)) { foreach ($attributes as $key => $val) { if (empty($val)) { unset($attributes[$key]); } } } if (empty($attributes)) { throw new \Magento\Framework\Exception\LocalizedException($this->getSpecifyOptionMessage()); } } return $this; }
/** * Check if product can be bought * * @param \Magento\Catalog\Model\Product $product * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function checkProductBuyState($product) { parent::checkProductBuyState($product); $productOptionIds = $this->getOptionsIds($product); $productSelections = $this->getSelectionsCollection($productOptionIds, $product); $selectionIds = $product->getCustomOption('bundle_selection_ids'); $selectionIds = unserialize($selectionIds->getValue()); $buyRequest = $product->getCustomOption('info_buyRequest'); $buyRequest = new \Magento\Framework\DataObject(unserialize($buyRequest->getValue())); $bundleOption = $buyRequest->getBundleOption(); if (empty($bundleOption)) { throw new \Magento\Framework\Exception\LocalizedException($this->getSpecifyOptionMessage()); } $skipSaleableCheck = $this->_catalogProduct->getSkipSaleableCheck(); foreach ($selectionIds as $selectionId) { /* @var $selection \Magento\Bundle\Model\Selection */ $selection = $productSelections->getItemById($selectionId); if (!$selection || !$selection->isSalable() && !$skipSaleableCheck) { throw new \Magento\Framework\Exception\LocalizedException(__('The required options you selected are not available.')); } } $product->getTypeInstance()->setStoreFilter($product->getStoreId(), $product); $optionsCollection = $this->getOptionsCollection($product); foreach ($optionsCollection->getItems() as $option) { if ($option->getRequired() && empty($bundleOption[$option->getId()])) { throw new \Magento\Framework\Exception\LocalizedException(__('Please select all required options.')); } } return $this; }