quoteRepository = $quoteRepository; $this->giftMessageManager = $giftMessageManager; $this->storeManager = $storeManager; $this->helper = $helper; $this->messageFactory = $messageFactory; } /** * @inheritdoc */ public function get($cartId, $itemId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!$item = $quote->getItemById($itemId)) { throw new NoSuchEntityException( __('No item with the provided ID was found in the Cart. Verify the ID and try again.') ); } $messageId = $item->getGiftMessageId(); if (!$messageId) { return null; } /** * Model. * * @var \Magento\GiftMessage\Model\Message $model */ return $this->messageFactory->create()->load($messageId); } /** * @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( __( 'No product with the "%1" itemId exists in the Cart. Verify your information and try again.', $itemId ) ); } if ($item->getIsVirtual()) { throw new InvalidTransitionException(__('Gift messages can\'t be used for virtual products.')); } $messageText = $giftMessage->getMessage(); if ($messageText && !$this->helper->isMessagesAllowed('items', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__("The gift message isn't available.")); } $this->giftMessageManager->setMessage($quote, 'quote_item', $giftMessage, $itemId); return true; } }