OrderSave.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\GiftMessage\Model\Plugin;
  8. use Magento\Framework\Exception\CouldNotSaveException;
  9. class OrderSave
  10. {
  11. /**
  12. * @var \Magento\GiftMessage\Api\OrderRepositoryInterface
  13. */
  14. protected $giftMessageOrderRepository;
  15. /**
  16. * @var \Magento\GiftMessage\Api\OrderItemRepositoryInterface
  17. */
  18. protected $giftMessageOrderItemRepository;
  19. /**
  20. * Init plugin
  21. *
  22. * @param \Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository
  23. * @param \Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository
  24. */
  25. public function __construct(
  26. \Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository,
  27. \Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository
  28. ) {
  29. $this->giftMessageOrderRepository = $giftMessageOrderRepository;
  30. $this->giftMessageOrderItemRepository = $giftMessageOrderItemRepository;
  31. }
  32. /**
  33. * Save gift message
  34. *
  35. * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
  36. * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
  37. * @return \Magento\Sales\Api\Data\OrderInterface
  38. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  39. * @throws CouldNotSaveException
  40. */
  41. public function afterSave(
  42. \Magento\Sales\Api\OrderRepositoryInterface $subject,
  43. \Magento\Sales\Api\Data\OrderInterface $resultOrder
  44. ) {
  45. /** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */
  46. $resultOrder = $this->saveOrderGiftMessage($resultOrder);
  47. $resultOrder = $this->saveOrderItemGiftMessage($resultOrder);
  48. return $resultOrder;
  49. }
  50. /**
  51. * Save gift message for order
  52. *
  53. * @param \Magento\Sales\Api\Data\OrderInterface $order
  54. * @return \Magento\Sales\Api\Data\OrderInterface
  55. * @throws CouldNotSaveException
  56. */
  57. protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
  58. {
  59. $extensionAttributes = $order->getExtensionAttributes();
  60. if (null !== $extensionAttributes &&
  61. null !== $extensionAttributes->getGiftMessage()
  62. ) {
  63. /* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
  64. $giftMessage = $extensionAttributes->getGiftMessage();
  65. try {
  66. $this->giftMessageOrderRepository->save($order->getEntityId(), $giftMessage);
  67. } catch (\Exception $e) {
  68. throw new CouldNotSaveException(
  69. __('The gift message couldn\'t be added to the "%1" order.', $e->getMessage()),
  70. $e
  71. );
  72. }
  73. }
  74. return $order;
  75. }
  76. /**
  77. * Save gift message for items of order
  78. *
  79. * @param \Magento\Sales\Api\Data\OrderInterface $order
  80. * @return \Magento\Sales\Api\Data\OrderInterface
  81. * @throws CouldNotSaveException
  82. */
  83. protected function saveOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
  84. {
  85. $items = $order->getItems();
  86. if (null !== $items) {
  87. /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
  88. foreach ($items as $orderItem) {
  89. $extensionAttribute = $orderItem->getExtensionAttributes();
  90. if (null !== $extensionAttribute &&
  91. null !== $extensionAttribute->getGiftMessage()
  92. ) {
  93. /* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
  94. $giftMessage = $extensionAttribute->getGiftMessage();
  95. try {
  96. $this->giftMessageOrderItemRepository->save(
  97. $order->getEntityId(),
  98. $orderItem->getItemId(),
  99. $giftMessage
  100. );
  101. } catch (\Exception $e) {
  102. throw new CouldNotSaveException(
  103. __('The gift message couldn\'t be added to the "%1" order item.', $e->getMessage()),
  104. $e
  105. );
  106. }
  107. }
  108. }
  109. }
  110. return $order;
  111. }
  112. }