Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SalesEventOrderItemToQuoteItemObserver.php
Go to the documentation of this file.
1 <?php
7 
9 
16 {
22  protected $_giftMessageMessage = null;
23 
27  protected $_messageFactory;
28 
33  public function __construct(
34  \Magento\GiftMessage\Model\MessageFactory $messageFactory,
35  \Magento\GiftMessage\Helper\Message $giftMessageMessage
36  ) {
37  $this->_messageFactory = $messageFactory;
38  $this->_giftMessageMessage = $giftMessageMessage;
39  }
40 
47  public function execute(\Magento\Framework\Event\Observer $observer)
48  {
50  $orderItem = $observer->getEvent()->getOrderItem();
51  // Do not import giftmessage data if order is reordered
52  $order = $orderItem->getOrder();
53  if ($order && $order->getReordered()) {
54  return $this;
55  }
56 
57  $isAvailable = $this->_giftMessageMessage->isMessagesAllowed(
58  'order_item',
59  $orderItem,
60  $orderItem->getStoreId()
61  );
62  if (!$isAvailable) {
63  return $this;
64  }
65 
67  $quoteItem = $observer->getEvent()->getQuoteItem();
68  if ($giftMessageId = $orderItem->getGiftMessageId()) {
69  $giftMessage = $this->_messageFactory->create()->load($giftMessageId)->setId(null)->save();
70  $quoteItem->setGiftMessageId($giftMessage->getId());
71  }
72 
73  return $this;
74  }
75 }
__construct(\Magento\GiftMessage\Model\MessageFactory $messageFactory, \Magento\GiftMessage\Helper\Message $giftMessageMessage)
$orderItem
Definition: order.php:30
$giftMessage
Definition: items.phtml:47
$order
Definition: order.php:55
$quoteItem
Definition: quote.php:38
$messageFactory
Definition: messages.php:10