85 public function get($orderId, $orderItemId)
88 if (!
$orderItem = $this->getItemById($orderId, $orderItemId)) {
90 __(
'No item with the provided ID was found in the Order. Verify the ID and try again.')
94 if (!$this->helper->isMessagesAllowed(
'order_item',
$orderItem, $this->storeManager->getStore())) {
95 throw new NoSuchEntityException(
97 "No item with the provided ID was found in the Order, or a gift message isn't allowed. " 98 .
"Verify and try again." 105 throw new NoSuchEntityException(
106 __(
'No item with the provided ID was found in the Order. Verify the ID and try again.')
110 return $this->messageFactory->create()->load($messageId);
119 $order = $this->orderFactory->create()->load($orderId);
122 if (!
$orderItem = $this->getItemById($orderId, $orderItemId)) {
123 throw new NoSuchEntityException(
124 __(
'No item with the provided ID was found in the Order. Verify the ID and try again.')
128 if (
$order->getIsVirtual()) {
129 throw new InvalidTransitionException(
__(
"Gift messages can't be used for virtual products."));
131 if (!$this->helper->isMessagesAllowed(
'order_item',
$orderItem, $this->storeManager->getStore())) {
132 throw new CouldNotSaveException(
__(
"The gift message isn't available."));
137 'type' =>
'order_item',
143 $this->giftMessageSaveModel->setGiftmessages(
$message);
145 $this->giftMessageSaveModel->saveAllInOrder();
146 unset($this->orders[$orderId]);
147 }
catch (\Exception $e) {
148 throw new CouldNotSaveException(
149 __(
'The gift message couldn\'t be added to the "%1" order.', $e->getMessage()),
163 protected function getItemById($orderId, $orderItemId)
165 if (!isset($this->orders[$orderId])) {
166 $this->orders[$orderId] = $this->orderFactory->create()->load($orderId);
169 $order = $this->orders[$orderId];
173 if (
$item !==
null) {
save($orderId, $orderItemId, \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)