Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderRepository.php
Go to the documentation of this file.
1 <?php
9 
14 
19 {
25  protected $orderFactory;
26 
32  protected $storeManager;
33 
40 
46  protected $helper;
47 
53  protected $messageFactory;
54 
62  public function __construct(
63  \Magento\Sales\Model\OrderFactory $orderFactory,
64  \Magento\Store\Model\StoreManagerInterface $storeManager,
65  \Magento\GiftMessage\Model\Save $giftMessageSaveModel,
66  \Magento\GiftMessage\Helper\Message $helper,
67  \Magento\GiftMessage\Model\MessageFactory $messageFactory
68  ) {
69  $this->orderFactory = $orderFactory;
70  $this->giftMessageSaveModel = $giftMessageSaveModel;
71  $this->storeManager = $storeManager;
72  $this->helper = $helper;
73  $this->messageFactory = $messageFactory;
74  }
75 
79  public function get($orderId)
80  {
82  $order = $this->orderFactory->create()->load($orderId);
83 
84  if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) {
85  throw new NoSuchEntityException(
86  __("Either no order exists with this ID or gift message isn't allowed.")
87  );
88  }
89 
90  $messageId = $order->getGiftMessageId();
91  if (!$messageId) {
92  throw new NoSuchEntityException(
93  __('No item with the provided ID was found in the Order. Verify the ID and try again.')
94  );
95  }
96 
97  return $this->messageFactory->create()->load($messageId);
98  }
99 
103  public function save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
104  {
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.'));
109  };
110 
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.")
114  );
115  }
116 
117  if ($order->getIsVirtual()) {
118  throw new InvalidTransitionException(__("Gift messages can't be used for virtual products."));
119  }
120  if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) {
121  throw new CouldNotSaveException(__("The gift message isn't available."));
122  }
123 
124  $message = [];
125  $message[$orderId] = [
126  'type' => 'order',
127  $giftMessage::CUSTOMER_ID => $giftMessage->getCustomerId(),
128  $giftMessage::SENDER => $giftMessage->getSender(),
129  $giftMessage::RECIPIENT => $giftMessage->getRecipient(),
130  $giftMessage::MESSAGE => $giftMessage->getMessage(),
131  ];
132 
133  $this->giftMessageSaveModel->setGiftmessages($message);
134  try {
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()),
139  $e
140  );
141  }
142  return true;
143  }
144 }
save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
$giftMessage
Definition: items.phtml:47
$order
Definition: order.php:55
__()
Definition: __.php:13
$message
__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)