/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage, $itemId) { $quote = $this->quoteRepository->get($cartId); /** @var CartItemInterface $item */ $item = $this->getQuoteItem($cartId, $itemId); if ($item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } $giftMessage->setCustomerId($quote->getCustomer()->getId()); $giftMessage->setGiftMessageId(rand()); $msgCacheId = $itemId . self::CACHE_ID_POSTFIX; $this->cache->save(serialize($giftMessage), $msgCacheId); return true; }
/** * {@inheritDoc} */ public function save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->orderFactory->create()->load($orderId); if (!$order->getEntityId()) { throw new NoSuchEntityException(__('There is no order with provided id')); } if (0 == $order->getTotalItemCount()) { throw new InputException(__('Gift Messages is not applicable for empty order')); } if ($order->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $message = []; $message[$orderId] = ['type' => 'order', $giftMessage::CUSTOMER_ID => $giftMessage->getCustomerId(), $giftMessage::SENDER => $giftMessage->getSender(), $giftMessage::RECIPIENT => $giftMessage->getRecipient(), $giftMessage::MESSAGE => $giftMessage->getMessage()]; $this->giftMessageSaveModel->setGiftmessages($message); try { $this->giftMessageSaveModel->saveAllInOrder(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to order: "%1"', $e->getMessage()), $e); } return true; }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** @var CartInterface $quote */ $quote = $this->quoteRepository->get($cartId); if (0 == $quote->getItemsCount()) { throw new InputException(__('Gift Messages is not applicable for empty cart')); } if ($quote->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } $giftMessage->setCustomerId($quote->getCustomer()->getId()); $giftMessage->setGiftMessageId(rand()); $msgCacheId = $cartId . self::CACHE_ID_POSTFIX; $this->cache->save(serialize($giftMessage), $msgCacheId); return true; }
public function testSave() { $this->quoteRepositoryMock->expects($this->any())->method('get')->willReturn($this->quoteMock); $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(1); $this->quoteMock->expects($this->once())->method('getIsVirtual')->willReturn(false); $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->cartRepository->save($this->cartId, $this->messageMock)); }
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)); }
/** * {@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')); } $messageText = $giftMessage->getMessage(); if ($messageText && !$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote', $giftMessage); return true; }
/** * {@inheritDoc} */ public function save($orderId, $orderItemId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->orderFactory->create()->load($orderId); /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */ if (!($orderItem = $this->getItemById($orderId, $orderItemId))) { throw new NoSuchEntityException(__('There is no item with provided id in the order')); } if ($order->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $message = []; $message[$orderItemId] = ['type' => 'order_item', 'sender' => $giftMessage->getSender(), 'recipient' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; $this->giftMessageSaveModel->setGiftmessages($message); try { $this->giftMessageSaveModel->saveAllInOrder(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to order: "%1"', $e->getMessage()), $e); } return true; }
/** * {@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')); } $messageText = $giftMessage->getMessage(); if ($messageText && !$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; }
/** * Sets the gift message to item or quote. * * @param \Magento\Quote\Model\Quote $quote The quote. * @param string $type The type. * @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message. * @param null|int $entityId The entity ID. * @return void * @throws \Magento\Framework\Exception\CouldNotSaveException The specified gift message is not available. * @throws \Magento\Framework\Exception\State\InvalidTransitionException The billing or shipping address is not set. */ public function setMessage(\Magento\Quote\Model\Quote $quote, $type, $giftMessage, $entityId = null) { if ($quote->getBillingAddress()->getCountryId() === null) { throw new InvalidTransitionException(__('Billing address is not set')); } // check if shipping address is set if ($quote->getShippingAddress()->getCountryId() === null) { throw new InvalidTransitionException(__('Shipping address is not set')); } $message[$type][$entityId] = ['from' => $giftMessage->getSender(), 'to' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; try { $this->add($message, $quote); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to shopping cart')); } }
/** * Sets the gift message to item or quote. * * @param \Magento\Quote\Model\Quote $quote The quote. * @param string $type The type. * @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message. * @param null|int $entityId The entity ID. * @return void * @throws \Magento\Framework\Exception\CouldNotSaveException The specified gift message is not available. */ public function setMessage(\Magento\Quote\Model\Quote $quote, $type, $giftMessage, $entityId = null) { $message[$type][$entityId] = ['from' => $giftMessage->getSender(), 'to' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; try { $this->add($message, $quote); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to shopping cart')); } }