/** * @param \Magento\Checkout\Model\Type\Onepage $subject * @param array $result * @return $this */ public function afterSaveShippingMethod(\Magento\Checkout\Model\Type\Onepage $subject, array $result) { if (!$result) { $giftMessages = $this->request->getParam('giftmessage'); $quote = $subject->getQuote(); $this->message->add($giftMessages, $quote); } return $result; }
/** * @expectedException \Magento\Framework\Exception\CouldNotSaveException * @expectedExceptionMessage Could not add gift message to shopping cart */ public function testSetMessageCouldNotAddGiftMessageException() { $this->giftMessageMock->expects($this->once())->method('getSender')->will($this->returnValue('sender')); $this->giftMessageMock->expects($this->once())->method('getRecipient')->will($this->returnValue('recipient')); $this->giftMessageMock->expects($this->once())->method('getMessage')->will($this->returnValue('Message')); $this->messageFactoryMock->expects($this->once())->method('create')->willThrowException(new \Exception()); $this->model->setMessage($this->quoteMock, 'item', $this->giftMessageMock); }
/** * Set gift message to item or quote * * @param \Magento\Sales\Model\Quote $quote * @param string $type * @param \Magento\GiftMessage\Service\V1\Data\Message $giftMessage * @param null|int $entityId * @return void * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\State\InvalidTransitionException */ protected function setMessage(\Magento\Sales\Model\Quote $quote, $type, $giftMessage, $entityId = null) { if (is_null($quote->getBillingAddress()->getCountryId())) { throw new InvalidTransitionException('Billing address is not set'); } // check if shipping address is set if (is_null($quote->getShippingAddress()->getCountryId())) { throw new InvalidTransitionException('Shipping address is not set'); } $configType = $type == 'quote' ? '' : 'items'; if (!$this->helper->getIsMessagesAvailable($configType, $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException('Gift Message is not available'); } $message[$type][$entityId] = ['from' => $giftMessage->getSender(), 'to' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; try { $this->giftMessageManager->add($message, $quote); } catch (\Exception $e) { throw new CouldNotSaveException('Could not add gift message to shopping cart'); } }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage, $itemId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!($item = $quote->getItemById($itemId))) { throw new NoSuchEntityException(__('There is no product with provided itemId: %1 in the cart', $itemId)); } if ($item->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('items', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote_item', $giftMessage, $itemId); return true; }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (0 == $quote->getItemsCount()) { throw new InputException(__('Gift Messages is not applicable for empty cart')); } if ($quote->isVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote', $giftMessage); return true; }
/** * @param \Magento\Multishipping\Model\Checkout\Type\Multishipping $subject * @param array|null $methods * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSetShippingMethods(\Magento\Multishipping\Model\Checkout\Type\Multishipping $subject, $methods) { $giftMessages = $this->request->getParam('giftmessage'); $quote = $subject->getQuote(); $this->message->add($giftMessages, $quote); }