79 public function get($orderId)
82 $order = $this->orderFactory->create()->load($orderId);
84 if (!$this->helper->isMessagesAllowed(
'order',
$order, $this->storeManager->getStore())) {
86 __(
"Either no order exists with this ID or gift message isn't allowed.")
90 $messageId =
$order->getGiftMessageId();
92 throw new NoSuchEntityException(
93 __(
'No item with the provided ID was found in the Order. Verify the ID and try again.')
97 return $this->messageFactory->create()->load($messageId);
106 $order = $this->orderFactory->create()->load($orderId);
107 if (!
$order->getEntityId()) {
108 throw new NoSuchEntityException(
__(
'No order exists with this ID. Verify your information and try again.'));
111 if (0 ==
$order->getTotalItemCount()) {
112 throw new InputException(
113 __(
"Gift messages can't be used for an empty order. Create an order, add an item, and try again.")
117 if (
$order->getIsVirtual()) {
118 throw new InvalidTransitionException(
__(
"Gift messages can't be used for virtual products."));
120 if (!$this->helper->isMessagesAllowed(
'order',
$order, $this->storeManager->getStore())) {
121 throw new CouldNotSaveException(
__(
"The gift message isn't available."));
127 $giftMessage::CUSTOMER_ID =>
$giftMessage->getCustomerId(),
129 $giftMessage::RECIPIENT =>
$giftMessage->getRecipient(),
133 $this->giftMessageSaveModel->setGiftmessages(
$message);
135 $this->giftMessageSaveModel->saveAllInOrder();
136 }
catch (\Exception $e) {
137 throw new CouldNotSaveException(
138 __(
'The gift message couldn\'t be added to the "%1" order.', $e->getMessage()),
save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
__construct(\Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\GiftMessage\Model\Save $giftMessageSaveModel, \Magento\GiftMessage\Helper\Message $helper, \Magento\GiftMessage\Model\MessageFactory $messageFactory)