From e90f80627de1c3c9c97352426b7f1fa9c2139d15 Mon Sep 17 00:00:00 2001 From: Arjen Miedema Date: Mon, 30 Sep 2024 10:39:16 +0200 Subject: [PATCH] Add logic to handle repair products (#36) --- src/Modifiers/Order/SetOrderItems.php | 38 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Modifiers/Order/SetOrderItems.php b/src/Modifiers/Order/SetOrderItems.php index 94f25df..0a25c81 100644 --- a/src/Modifiers/Order/SetOrderItems.php +++ b/src/Modifiers/Order/SetOrderItems.php @@ -15,6 +15,7 @@ use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\Event\ManagerInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Model\ShippingAssignmentFactory; @@ -37,7 +38,8 @@ public function __construct( private readonly ProductRepositoryInterface $productRepository, private readonly ItemFactory $itemFactory, private readonly OrderItemRepositoryInterface $orderItemRepository, - private readonly OrderItemExtensionFactory $extensionFactory + private readonly OrderItemExtensionFactory $extensionFactory, + private readonly ManagerInterface $eventManager, ) { parent::__construct( $orderRepository, @@ -70,23 +72,30 @@ public function process(mixed $model, mixed $result): mixed continue; } - $orderItem = $this->getOrderItemFromExternalOrder($item, (int) $result->getEntityId()) ?? $this->itemFactory->create(); - - if (!$orderItem->getItemId()) { - $this->setBasicOrderItemData($orderItem, $product, $item) - ->setOrderItemPriceData($orderItem, $product, $item); - } - - // phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge + $orderItem = $this->getOrderItemFromExternalOrder($item, (int) $result->getEntityId()) ?? $this->itemFactory->create(); $additionalData = array_reduce( $item->getAdditionalData(), + // phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge static fn (array $carry, AdditionalDataInterface $data) => array_merge( $carry, [$data->getKey() => $data->getValue()] ), [] ); - // phpcs:enable + + if (!$orderItem->getItemId()) { + $this->setBasicOrderItemData($orderItem, $product, $item) + ->setOrderItemPriceData($orderItem, $product, $item); + } + + $this->eventManager->dispatch( + 'exact_order_set_order_item_before', + [ + 'item' => $orderItem, + 'external_order_item' => $item, + 'order' => $result + ] + ); $extensionAttributes = $orderItem->getExtensionAttributes() ?: $this->extensionFactory->create(); $extensionAttributes->setExpectedDeliveryDate($additionalData['expected_delivery_date'] ?? null) @@ -147,16 +156,13 @@ private function setOrderItemPriceData( return $this; } - private function getOrderItemFromExternalOrder(ItemInterface $item, ?int $orderId): ?OrderItemInterface + private function getOrderItemFromExternalOrder(ItemInterface $item, int $orderId): ?OrderItemInterface { $searchCriteria = $this->searchCriteriaBuilder ->addFilter(OrderItemInterface::SKU, $item->getSku()) ->addFilter(OrderItemInterface::QTY_ORDERED, $item->getQty()) - ->addFilter(OrderItemInterface::ROW_TOTAL, $item->getRowTotal()); - - if ($orderId !== null) { - $searchCriteria->addFilter(OrderItemInterface::ORDER_ID, $orderId); - } + ->addFilter(OrderItemInterface::ROW_TOTAL, $item->getRowTotal()) + ->addFilter(OrderItemInterface::ORDER_ID, $orderId); return current( $this->orderItemRepository