Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderItemRepository.php
Go to the documentation of this file.
1 <?php
9 
13 
18 {
24  protected $orderFactory;
25 
31  private $orders;
32 
38  protected $storeManager;
39 
46 
52  protected $helper;
53 
59  protected $messageFactory;
60 
68  public function __construct(
69  \Magento\Sales\Model\OrderFactory $orderFactory,
70  \Magento\Store\Model\StoreManagerInterface $storeManager,
71  \Magento\GiftMessage\Model\Save $giftMessageSaveModel,
72  \Magento\GiftMessage\Helper\Message $helper,
73  \Magento\GiftMessage\Model\MessageFactory $messageFactory
74  ) {
75  $this->orderFactory = $orderFactory;
76  $this->giftMessageSaveModel = $giftMessageSaveModel;
77  $this->storeManager = $storeManager;
78  $this->helper = $helper;
79  $this->messageFactory = $messageFactory;
80  }
81 
85  public function get($orderId, $orderItemId)
86  {
88  if (!$orderItem = $this->getItemById($orderId, $orderItemId)) {
89  throw new NoSuchEntityException(
90  __('No item with the provided ID was found in the Order. Verify the ID and try again.')
91  );
92  };
93 
94  if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) {
95  throw new NoSuchEntityException(
96  __(
97  "No item with the provided ID was found in the Order, or a gift message isn't allowed. "
98  . "Verify and try again."
99  )
100  );
101  }
102 
103  $messageId = $orderItem->getGiftMessageId();
104  if (!$messageId) {
105  throw new NoSuchEntityException(
106  __('No item with the provided ID was found in the Order. Verify the ID and try again.')
107  );
108  }
109 
110  return $this->messageFactory->create()->load($messageId);
111  }
112 
116  public function save($orderId, $orderItemId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
117  {
119  $order = $this->orderFactory->create()->load($orderId);
120 
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.')
125  );
126  };
127 
128  if ($order->getIsVirtual()) {
129  throw new InvalidTransitionException(__("Gift messages can't be used for virtual products."));
130  }
131  if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) {
132  throw new CouldNotSaveException(__("The gift message isn't available."));
133  }
134 
135  $message = [];
136  $message[$orderItemId] = [
137  'type' => 'order_item',
138  'sender' => $giftMessage->getSender(),
139  'recipient' => $giftMessage->getRecipient(),
140  'message' => $giftMessage->getMessage(),
141  ];
142 
143  $this->giftMessageSaveModel->setGiftmessages($message);
144  try {
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()),
150  $e
151  );
152  }
153  return true;
154  }
155 
163  protected function getItemById($orderId, $orderItemId)
164  {
165  if (!isset($this->orders[$orderId])) {
166  $this->orders[$orderId] = $this->orderFactory->create()->load($orderId);
167  }
169  $order = $this->orders[$orderId];
171  $item = $order->getItemById($orderItemId);
172 
173  if ($item !== null) {
174  return $item;
175  }
176  return false;
177  }
178 }
$orderItem
Definition: order.php:30
$giftMessage
Definition: items.phtml:47
$order
Definition: order.php:55
__()
Definition: __.php:13
$message
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)