/**
  * {@inheritdoc}
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     /** @var $quoteIdMask QuoteIdMask */
     $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartItem->getQuoteId(), 'masked_id');
     $cartItem->setQuoteId($quoteIdMask->getQuoteId());
     return $this->repository->save($cartItem);
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function processOptions(CartItemInterface $cartItem)
 {
     if ($cartItem->getProductType() !== \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         return $cartItem;
     }
     $productOptions = [];
     $bundleOptions = $cartItem->getBuyRequest()->getBundleOption();
     $bundleOptionsQty = $cartItem->getBuyRequest()->getBundleOptionQty();
     foreach ($bundleOptions as $optionId => $optionSelections) {
         if (empty($optionSelections)) {
             continue;
         }
         $optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections];
         $optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1;
         /** @var \Magento\Bundle\Api\Data\BundleOptionInterface $productOption */
         $productOption = $this->bundleOptionFactory->create();
         $productOption->setOptionId($optionId);
         $productOption->setOptionSelections($optionSelections);
         $productOption->setOptionQty($optionQty);
         $productOptions[] = $productOption;
     }
     $extension = $this->productOptionExtensionFactory->create()->setBundleOptions($productOptions);
     if (!$cartItem->getProductOption()) {
         $cartItem->setProductOption($this->productOptionFactory->create());
     }
     $cartItem->getProductOption()->setExtensionAttributes($extension);
     return $cartItem;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     $qty = $cartItem->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $cartItem->getQuoteId();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $itemId = $cartItem->getItemId();
     try {
         /** update item */
         if (isset($itemId)) {
             $item = $quote->getItemById($itemId);
             if (!$item) {
                 throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item  %2', $cartId, $itemId));
             }
             $productType = $item->getProduct()->getTypeId();
             $buyRequestData = $this->getBuyRequest($productType, $cartItem);
             if (is_object($buyRequestData)) {
                 /** update item product options */
                 /** @var  \Magento\Quote\Model\Quote\Item $cartItem */
                 $cartItem = $quote->updateItem($itemId, $buyRequestData);
             } else {
                 /** update item qty */
                 $item->setData('qty', $qty);
             }
         } else {
             /** add item to shopping cart */
             $product = $this->productRepository->get($cartItem->getSku());
             $productType = $product->getTypeId();
             /** @var  \Magento\Quote\Model\Quote\Item|string $cartItem */
             $cartItem = $quote->addProduct($product, $this->getBuyRequest($productType, $cartItem));
             if (is_string($cartItem)) {
                 throw new \Magento\Framework\Exception\LocalizedException(__($cartItem));
             }
         }
         $this->quoteRepository->save($quote->collectTotals());
     } catch (\Exception $e) {
         if ($e instanceof NoSuchEntityException || $e instanceof LocalizedException) {
             throw $e;
         }
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     $itemId = $cartItem->getId();
     foreach ($quote->getAllItems() as $quoteItem) {
         if ($itemId == $quoteItem->getId()) {
             $cartItem = $this->addProductOptions($productType, $quoteItem);
             return $this->applyCustomOptions($cartItem);
         }
     }
     throw new CouldNotSaveException(__('Could not save quote'));
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $cartId = $cartItem->getQuoteId();
     $quote = $this->quoteRepository->getActive($cartId);
     $quoteItems = $quote->getItems();
     $quoteItems[] = $cartItem;
     $quote->setItems($quoteItems);
     $this->quoteRepository->save($quote);
     $quote->collectTotals();
     return $quote->getLastAddedItem();
 }
Exemplo n.º 5
0
 /**
  * @param CartInterface $quote
  * @param CartItemInterface $item
  * @return CartItemInterface
  * @throws CouldNotSaveException
  * @throws InputException
  * @throws LocalizedException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save(CartInterface $quote, CartItemInterface $item)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $qty = $item->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $item->getQuoteId();
     $itemId = $item->getItemId();
     try {
         /** Update existing item */
         if (isset($itemId)) {
             $currentItem = $quote->getItemById($itemId);
             if (!$currentItem) {
                 throw new NoSuchEntityException(__('Cart %1 does not contain item %2', $cartId, $itemId));
             }
             $productType = $currentItem->getProduct()->getTypeId();
             $buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item);
             if (is_object($buyRequestData)) {
                 /** Update item product options */
                 $item = $quote->updateItem($itemId, $buyRequestData);
             } else {
                 if ($item->getQty() !== $currentItem->getQty()) {
                     $currentItem->setQty($qty);
                 }
             }
         } else {
             /** add new item to shopping cart */
             $product = $this->productRepository->get($item->getSku());
             $productType = $product->getTypeId();
             $item = $quote->addProduct($product, $this->cartItemOptionProcessor->getBuyRequest($productType, $item));
             if (is_string($item)) {
                 throw new LocalizedException(__($item));
             }
         }
     } catch (NoSuchEntityException $e) {
         throw $e;
     } catch (LocalizedException $e) {
         throw $e;
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     $itemId = $item->getId();
     foreach ($quote->getAllItems() as $quoteItem) {
         /** @var \Magento\Quote\Model\Quote\Item $quoteItem */
         if ($itemId == $quoteItem->getId()) {
             $item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem);
             return $this->cartItemOptionProcessor->applyCustomOptions($item);
         }
     }
     throw new CouldNotSaveException(__('Could not save quote'));
 }
Exemplo n.º 6
0
 public function testCollect()
 {
     $this->shippingAssignment->expects($this->exactly(3))->method('getShipping')->willReturn($this->shipping);
     $this->shipping->expects($this->exactly(2))->method('getAddress')->willReturn($this->address);
     $this->shipping->expects($this->once())->method('getMethod')->willReturn('flatrate');
     $this->shippingAssignment->expects($this->atLeastOnce())->method('getItems')->willReturn([$this->cartItem]);
     $this->freeShipping->expects($this->once())->method('isFreeShipping')->with($this->quote, [$this->cartItem])->willReturn(true);
     $this->address->expects($this->once())->method('setFreeShipping')->with(true);
     $this->total->expects($this->atLeastOnce())->method('setTotalAmount');
     $this->total->expects($this->atLeastOnce())->method('setBaseTotalAmount');
     $this->cartItem->expects($this->atLeastOnce())->method('getProduct')->willReturnSelf();
     $this->cartItem->expects($this->atLeastOnce())->method('isVirtual')->willReturn(false);
     $this->cartItem->expects($this->once())->method('getParentItem')->willReturn(false);
     $this->cartItem->expects($this->once())->method('getHasChildren')->willReturn(false);
     $this->cartItem->expects($this->once())->method('getWeight')->willReturn(2);
     $this->cartItem->expects($this->atLeastOnce())->method('getQty')->willReturn(2);
     $this->address->expects($this->once())->method('getFreeShipping')->willReturn(true);
     $this->cartItem->expects($this->once())->method('setRowWeight')->with(0);
     $this->address->expects($this->once())->method('setItemQty')->with(2);
     $this->address->expects($this->atLeastOnce())->method('setWeight');
     $this->address->expects($this->atLeastOnce())->method('setFreeMethodWeight');
     $this->address->expects($this->once())->method('collectShippingRates');
     $this->address->expects($this->once())->method('getAllShippingRates')->willReturn([$this->rate]);
     $this->rate->expects($this->once())->method('getCode')->willReturn('flatrate');
     $this->quote->expects($this->once())->method('getStore')->willReturn($this->store);
     $this->rate->expects($this->atLeastOnce())->method('getPrice')->willReturn(5);
     $this->priceCurrency->expects($this->once())->method('convert')->with(5, $this->store)->willReturn(5);
     $this->total->expects($this->once())->method('setShippingAmount')->with(5);
     $this->rate->expects($this->once())->method('getCarrierTitle')->willReturn('Carrier title');
     $this->rate->expects($this->once())->method('getMethodTitle')->willReturn('Method title');
     $this->address->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title');
     $this->address->expects($this->once())->method('getShippingDescription')->willReturn('Carrier title - Method title');
     $this->total->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title');
     $this->shippingModel->collect($this->quote, $this->shippingAssignment, $this->total);
 }
 public function testProcessCustomOptions()
 {
     $optionId = 23;
     $quoteItemOption = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Item\\Option')->disableOriginalConstructor()->getMock();
     $this->cartItem->expects($this->atLeastOnce())->method('getOptionByCode')->with('info_buyRequest')->willReturn($quoteItemOption);
     $quoteItemOption->expects($this->once())->method('getValue')->willReturn('a:1:{s:7:"options";a:1:{i:' . $optionId . ';a:2:{i:0;s:1:"5";i:1;s:1:"6";}}} ');
     $this->customOptionFactory->expects($this->once())->method('create')->willReturn($this->customOption);
     $this->customOption->expects($this->once())->method('setOptionId')->with($optionId);
     $this->customOption->expects($this->once())->method('setOptionValue')->with('5,6');
     $this->cartItem->expects($this->atLeastOnce())->method('getProductOption')->willReturn(false);
     $this->productOptionFactory->expects($this->once())->method('create')->willReturn($this->productOption);
     $this->productOption->expects($this->once())->method('getExtensionAttributes')->willReturn(false);
     $this->extensionFactory->expects($this->once())->method('create')->willReturn($this->extensibleAttribute);
     $this->extensibleAttribute->expects($this->once())->method('setCustomOptions')->with([$optionId => $this->customOption]);
     $this->productOption->expects($this->once())->method('setExtensionAttributes')->with($this->extensibleAttribute);
     $this->cartItem->expects($this->once())->method('setProductOption')->with($this->productOption);
     $this->assertSame($this->cartItem, $this->processor->processOptions($this->cartItem));
 }
Exemplo n.º 8
0
 public function testSave()
 {
     $this->quoteRepositoryMock->expects($this->any())->method('get')->willReturn($this->quoteMock);
     $this->quoteItemMock->expects($this->once())->method('getItemId')->willReturn($this->itemId);
     $this->quoteItemMock->expects($this->once())->method('getProductType')->willReturn(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
     $customerMock = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false);
     $this->quoteMock->expects($this->once())->method('getCustomer')->willReturn($customerMock);
     $this->messageMock->expects($this->any())->method('setCustomerId');
     $this->messageMock->expects($this->any())->method('setGiftMessageId');
     $this->cacheMock->expects($this->once())->method('save');
     $this->assertTrue($this->itemRepository->save($this->cartId, $this->messageMock, $this->itemId));
 }
 /**
  * Process cart item product options
  *
  * @param CartItemInterface $cartItem
  * @return CartItemInterface
  */
 public function processOptions(CartItemInterface $cartItem)
 {
     $downloadableLinkIds = [];
     $option = $cartItem->getOptionByCode('downloadable_link_ids');
     if (!empty($option)) {
         $downloadableLinkIds = explode(',', $option->getValue());
     }
     $downloadableOption = $this->downloadableOptionFactory->create();
     $this->dataObjectHelper->populateWithArray($downloadableOption, ['downloadable_links' => $downloadableLinkIds], 'Magento\\Downloadable\\Api\\Data\\DownloadableOptionInterface');
     $productOption = $cartItem->getProductOption() ? $cartItem->getProductOption() : $this->productOptionFactory->create();
     $extensibleAttribute = $productOption->getExtensionAttributes() ? $productOption->getExtensionAttributes() : $this->extensionFactory->create();
     $extensibleAttribute->setDownloadableOption($downloadableOption);
     $productOption->setExtensionAttributes($extensibleAttribute);
     $cartItem->setProductOption($productOption);
     return $cartItem;
 }
 /**
  * {@inheritdoc}
  */
 public function processOptions(CartItemInterface $cartItem)
 {
     $attributesOption = $cartItem->getProduct()->getCustomOption('attributes');
     $selectedConfigurableOptions = unserialize($attributesOption->getValue());
     if (is_array($selectedConfigurableOptions)) {
         $configurableOptions = [];
         foreach ($selectedConfigurableOptions as $optionId => $optionValue) {
             /** @var \Magento\ConfigurableProduct\Api\Data\ConfigurableItemOptionValueInterface $option */
             $option = $this->itemOptionValueFactory->create();
             $option->setOptionId($optionId);
             $option->setOptionValue($optionValue);
             $configurableOptions[] = $option;
         }
         $productOption = $cartItem->getProductOption() ? $cartItem->getProductOption() : $this->productOptionFactory->create();
         /** @var  \Magento\Quote\Api\Data\ProductOptionExtensionInterface $extensibleAttribute */
         $extensibleAttribute = $productOption->getExtensionAttributes() ? $productOption->getExtensionAttributes() : $this->extensionFactory->create();
         $extensibleAttribute->setConfigurableItemOptions($configurableOptions);
         $productOption->setExtensionAttributes($extensibleAttribute);
         $cartItem->setProductOption($productOption);
     }
     return $cartItem;
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function saveForCustomer($customerId, \Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     $cart = $this->quoteRepository->getActiveForCustomer($customerId);
     $cartItem->setQuoteId($cart->getId());
     return $this->save($cartItem);
 }
 /**
  * Receive custom option from buy request
  *
  * @param CartItemInterface $cartItem
  * @return array
  */
 protected function getOptions(CartItemInterface $cartItem)
 {
     $buyRequest = !empty($cartItem->getOptionByCode('info_buyRequest')) ? unserialize($cartItem->getOptionByCode('info_buyRequest')->getValue()) : null;
     return is_array($buyRequest) && isset($buyRequest['options']) ? $buyRequest['options'] : [];
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function delete(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     $cartId = $cartItem->getQuoteId();
     $itemId = $cartItem->getItemId();
     /**
      * Quote.
      *
      * @var \Magento\Quote\Model\Quote $quote
      */
     $quote = $this->quoteRepository->getActive($cartId);
     $quoteItem = $quote->getItemById($itemId);
     if (!$quoteItem) {
         throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item  %2', $cartId, $itemId));
     }
     try {
         $quote->removeItem($itemId);
         $this->quoteRepository->save($quote->collectTotals());
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not remove item from quote'));
     }
 }
Exemplo n.º 14
0
 protected function freeShippingAssertions()
 {
     $this->address->expects($this->at(0))->method('getFreeShipping')->willReturn(false);
     $this->address->expects($this->at(1))->method('getFreeShipping')->willReturn(true);
     $this->cartItem->expects($this->atLeastOnce())->method('getFreeShipping')->willReturn(true);
 }