/** * 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; }
/** * Returns formatted buy request - object, holding request received from * product view page with keys and options for configured product * * @return \Magento\Framework\DataObject */ public function getBuyRequest() { $option = $this->getProductOptionByCode('info_buyRequest'); if (!$option) { $option = []; } $buyRequest = new \Magento\Framework\DataObject($option); $buyRequest->setQty($this->getQtyOrdered() * 1); return $buyRequest; }
public function getRequest() { $request = new \Magento\Framework\DataObject(); $request->setQty($this->proxyItem->getQty()); // grouped and downloadable products doesn't have options if ($this->proxyItem->getProduct()->getTypeId() == TypeGrouped::TYPE_CODE || $this->proxyItem->getProduct()->getTypeId() == TypeDownloadable::TYPE_DOWNLOADABLE) { return $request; } /** @var $magentoProduct \Ess\M2ePro\Model\Magento\Product */ $magentoProduct = $this->modelFactory->getObject('Magento\\Product')->setProduct($this->getProduct()); $options = $this->proxyItem->getOptions(); if (empty($options)) { return $request; } if ($magentoProduct->isSimpleType()) { $request->setOptions($options); } else { if ($magentoProduct->isBundleType()) { $request->setBundleOption($options); } else { if ($magentoProduct->isConfigurableType()) { $request->setSuperAttribute($options); } } } return $request; }
public function _populateQuoteItems(\Magento\Quote\Model\Quote $quote) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->_getProductBySku(self::SKU); // $product->setPrice(8.00); $req = new \Magento\Framework\DataObject(); $req->setQty(1); $req->setCustomPrice(8); $quote->addProduct($product, $req); return $quote; }