/**
  * {@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;
 }
 /**
  * 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'));
     }
 }
 /**
  * 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'));
     }
 }
 /**
  * {@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;
 }