Skip to content

Commit

Permalink
Add logic to handle repair products (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjenMiedema authored Sep 30, 2024
1 parent b9e9cbd commit e90f806
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/Modifiers/Order/SetOrderItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e90f806

Please sign in to comment.