Example #1
0
 /**
  * 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()));
         if (!$buyRequest->hasLinks()) {
             if (!$product->getLinksPurchasedSeparately()) {
                 $allLinksIds = $this->_linksFactory->create()->addProductToFilter($product->getId())->getAllIds();
                 $buyRequest->setLinks($allLinksIds);
                 $product->addCustomOption('info_buyRequest', serialize($buyRequest->getData()));
             } else {
                 throw new \Magento\Framework\Exception\LocalizedException(__('Please specify product link(s).'));
             }
         }
     }
     return $this;
 }