Exemple #1
0
 /**
  * Move quote item to another items list
  *
  * @param int|\Magento\Quote\Model\Quote\Item $item
  * @param string $moveTo
  * @param int $qty
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function moveQuoteItem($item, $moveTo, $qty)
 {
     $item = $this->_getQuoteItem($item);
     if ($item) {
         $removeItem = false;
         $moveTo = explode('_', $moveTo);
         switch ($moveTo[0]) {
             case 'order':
                 $info = $item->getBuyRequest();
                 $info->setOptions($this->_prepareOptionsForRequest($item))->setQty($qty);
                 $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product')->setStoreId($this->getQuote()->getStoreId())->load($item->getProduct()->getId());
                 $product->setSkipCheckRequiredOption(true);
                 $newItem = $this->getQuote()->addProduct($product, $info);
                 if (is_string($newItem)) {
                     throw new \Magento\Framework\Exception\LocalizedException(__($newItem));
                 }
                 $product->unsSkipCheckRequiredOption();
                 $newItem->checkData();
                 $this->_needCollectCart = true;
                 break;
             case 'cart':
                 $cart = $this->getCustomerCart();
                 if ($cart && is_null($item->getOptionByCode('additional_options'))) {
                     //options and info buy request
                     $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product')->setStoreId($this->getQuote()->getStoreId())->load($item->getProduct()->getId());
                     $info = $item->getOptionByCode('info_buyRequest');
                     if ($info) {
                         $info = new \Magento\Framework\DataObject(unserialize($info->getValue()));
                         $info->setQty($qty);
                         $info->setOptions($this->_prepareOptionsForRequest($item));
                     } else {
                         $info = new \Magento\Framework\DataObject(['product_id' => $product->getId(), 'qty' => $qty, 'options' => $this->_prepareOptionsForRequest($item)]);
                     }
                     $cartItem = $cart->addProduct($product, $info);
                     if (is_string($cartItem)) {
                         throw new \Magento\Framework\Exception\LocalizedException(__($cartItem));
                     }
                     $cartItem->setPrice($item->getProduct()->getPrice());
                     $this->_needCollectCart = true;
                     $removeItem = true;
                 }
                 break;
             case 'wishlist':
                 $wishlist = null;
                 if (!isset($moveTo[1])) {
                     $wishlist = $this->_objectManager->create('Magento\\Wishlist\\Model\\Wishlist')->loadByCustomerId($this->getSession()->getCustomerId(), true);
                 } else {
                     $wishlist = $this->_objectManager->create('Magento\\Wishlist\\Model\\Wishlist')->load($moveTo[1]);
                     if (!$wishlist->getId() || $wishlist->getCustomerId() != $this->getSession()->getCustomerId()) {
                         $wishlist = null;
                     }
                 }
                 if (!$wishlist) {
                     throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t find this wish list.'));
                 }
                 $wishlist->setStore($this->getSession()->getStore())->setSharedStoreIds($this->getSession()->getStore()->getWebsite()->getStoreIds());
                 if ($wishlist->getId() && $item->getProduct()->isVisibleInSiteVisibility()) {
                     $info = $item->getBuyRequest();
                     $info->setOptions($this->_prepareOptionsForRequest($item))->setQty($qty)->setStoreId($this->getSession()->getStoreId());
                     $wishlist->addNewItem($item->getProduct(), $info);
                     $removeItem = true;
                 }
                 break;
             case 'remove':
                 $removeItem = true;
                 break;
             default:
                 break;
         }
         if ($removeItem) {
             $this->getQuote()->deleteItem($item);
         }
         $this->setRecollect(true);
     }
     return $this;
 }
Exemple #2
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);
     $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;
 }