From 5c813cfabdb24ce960e49c83ae12c560241d2222 Mon Sep 17 00:00:00 2001 From: Christian Dangl Date: Mon, 23 Oct 2023 13:40:27 +0200 Subject: [PATCH] MOL-1235: Fix voucher recognition in bundles --- src/Service/MollieApi/OrderDataExtractor.php | 32 +++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Service/MollieApi/OrderDataExtractor.php b/src/Service/MollieApi/OrderDataExtractor.php index 9d468f896..4f84a5dd8 100644 --- a/src/Service/MollieApi/OrderDataExtractor.php +++ b/src/Service/MollieApi/OrderDataExtractor.php @@ -9,6 +9,7 @@ use Kiener\MolliePayments\Exception\OrderLineItemsNotFoundException; use Kiener\MolliePayments\Service\CustomerService; use Psr\Log\LoggerInterface; +use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection; use Shopware\Core\Checkout\Customer\CustomerEntity; use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity; use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryCollection; @@ -140,6 +141,11 @@ public function extractDelivery(OrderEntity $orderEntity, Context $context): Ord return $delivery; } + /** + * @param OrderEntity $orderEntity + * @param Context $context + * @return OrderLineItemCollection + */ public function extractLineItems(OrderEntity $orderEntity, Context $context): OrderLineItemCollection { $lineItems = $orderEntity->getLineItems(); @@ -152,6 +158,30 @@ public function extractLineItems(OrderEntity $orderEntity, Context $context): Or throw new OrderLineItemsNotFoundException($orderEntity->getId()); } - return $lineItems; + # we have to build a flat list + # so that we also check nested items in bundles for instance + return new OrderLineItemCollection( + [ + $this->buildFlat($lineItems) + ] + ); + } + + /** + * @param LineItemCollection $lineItems + * @return array + */ + private function buildFlat(LineItemCollection $lineItems): array + { + $flat = []; + foreach ($lineItems as $lineItem) { + $flat[] = $lineItem; + + foreach ($this->buildFlat($lineItem->getChildren()) as $nest) { + $flat[] = $nest; + } + } + + return $flat; } }