/** * Move quote item to another items list * * @param int|\Magento\Sales\Model\Quote\Item $item * @param string $moveTo * @param int $qty * @return $this * @throws \Magento\Framework\Model\Exception */ 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\Model\Exception($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\Object(unserialize($info->getValue())); $info->setQty($qty); $info->setOptions($this->_prepareOptionsForRequest($item)); } else { $info = new \Magento\Framework\Object(array('product_id' => $product->getId(), 'qty' => $qty, 'options' => $this->_prepareOptionsForRequest($item))); } $cartItem = $cart->addProduct($product, $info); if (is_string($cartItem)) { throw new \Magento\Framework\Model\Exception($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\Model\Exception(__('We couldn\'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; }
/** * Returns formatted buy request - object, holding request received from * product view page with keys and options for configured product * * @return \Magento\Framework\Object */ public function getBuyRequest() { $option = $this->getProductOptionByCode('info_buyRequest'); if (!$option) { $option = []; } $buyRequest = new \Magento\Framework\Object($option); $buyRequest->setQty($this->getQtyOrdered() * 1); return $buyRequest; }
/** * Get request for product add to cart procedure * * @param \Magento\Framework\Object|int|array $requestInfo * @return \Magento\Framework\Object */ protected function _getProductRequest($requestInfo) { if ($requestInfo instanceof \Magento\Framework\Object) { $request = $requestInfo; } elseif (is_numeric($requestInfo)) { $request = new \Magento\Framework\Object(array('qty' => $requestInfo)); } else { $request = new \Magento\Framework\Object($requestInfo); } if (!$request->hasQty()) { $request->setQty(1); } return $request; }