From 1d54356a5b8cbe7093325e2f9c54bcde74ce2e62 Mon Sep 17 00:00:00 2001 From: Florian Eisenmenger Date: Fri, 6 May 2022 14:10:09 +0200 Subject: [PATCH] rabatt --- .../StateMachineTransitionSubscriber.php | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/AtlDiscountExtension-main/src/Subscriber/StateMachineTransitionSubscriber.php b/AtlDiscountExtension-main/src/Subscriber/StateMachineTransitionSubscriber.php index 28664af..5dd6a35 100644 --- a/AtlDiscountExtension-main/src/Subscriber/StateMachineTransitionSubscriber.php +++ b/AtlDiscountExtension-main/src/Subscriber/StateMachineTransitionSubscriber.php @@ -4,7 +4,9 @@ namespace Atl\DiscountExtension\Subscriber; +use Atl\DiscountExtension\Core\System\RepeatDiscount\RepeatDiscountCollection; use Atl\DiscountExtension\Core\System\RepeatDiscount\RepeatDiscountEntity; +use Psr\Log\LoggerInterface; use Shopware\Core\Checkout\Customer\CustomerEntity; use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection; use Shopware\Core\Checkout\Order\OrderEntity; @@ -16,6 +18,7 @@ use Shopware\Core\System\CustomField\CustomFieldEntity; use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent; use Shopware\Core\System\SystemConfig\SystemConfigService; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Serializer\Serializer; class StateMachineTransitionSubscriber implements EventSubscriberInterface { @@ -77,7 +80,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface $orderId = $event->getEntityId(); $order = $this->getOrder($orderId, $event); $salesChannelId = $order->getSalesChannelId(); - + // In case no order could be found - return if (empty($order)) { return; @@ -85,14 +88,14 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface $minAmountDiscountableLineItems = $this->systemConfigService->get('AtlDiscountExtension.config.minAmountDiscountableLineItems', $salesChannelId); $amountDiscountableLineItems = $this->getAmountDiscountableLineItems($order->getLineItems(), $minAmountDiscountableLineItems, $salesChannelId, $event); - + // In case it is not enough - return if ($amountDiscountableLineItems < $minAmountDiscountableLineItems) { return; } - + $customer = $order->getOrderCustomer()->getCustomer(); - + // Tag the customer and trace last order date in 'spwn_repeat_discount' table $this->tagCustomer($customer, $salesChannelId, $event); } @@ -107,7 +110,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface $criteria = new Criteria(); $criteria->addFilter(new EqualsFilter('id', $orderId)); $criteria->addAssociation('lineItems'); - $criteria->addAssociation('orderCustomer.customer'); + $criteria->addAssociation('orderCustomer.customer.repeatDiscounts'); return $this->orderRepository->search($criteria, $event->getContext())->first(); } @@ -133,15 +136,20 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface if ($customFieldName === '') { return 0; } - + $amountDiscountableLineItems = 0; foreach($lineItems as $lineItem) { + + // If no product lineItem - continue if (empty($lineItem->getPayload()['customFields'])) { continue; } + $customFields = $lineItem->getPayload()['customFields']; + // Checks if the custom field exists and is set to true - if (!empty($customFields[$customFieldName]) && $customFields[$customFieldName] === true) { + if ((!empty($customFields[$customFieldName]) && $customFields[$customFieldName] === true) && + (!empty($customFields['custom_weightlossplan_day1_breakfast']))) { $amountDiscountableLineItems += $lineItem->getQuantity(); } @@ -150,6 +158,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface break; } } + return $amountDiscountableLineItems; } @@ -197,19 +206,28 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface 'id' => $tagId ] ], - 'repeatDiscount' => [ - 'customerId' => $customer->getId(), - 'lastOrderDate' => new \DateTime() + 'repeatDiscounts' => [ + [ + 'customerId' => $customer->getId(), + 'tagId' => $tagId, + 'lastOrderDate' => new \DateTime() + ] ] ]; - /** @var RepeatDiscountEntity $repeatDiscount */ - $repeatDiscount = $customer->getExtensions()['repeatDiscount']; + /** @var RepeatDiscountCollection $repeatDiscounts */ + $repeatDiscounts = $customer->getExtensions()['repeatDiscounts']; // If a discount is already applied, update its duration, else create one - if (!empty($repeatDiscount)) { - $repeatDiscountId = $repeatDiscount->getId(); - $update['repeatDiscount']['id'] = $repeatDiscountId; + if (!empty($repeatDiscounts)) { + foreach ($repeatDiscounts as $repeatDiscount) { + $repeatDiscountTagId = $repeatDiscount->getTagId(); + + if ($repeatDiscountTagId === $tagId) { + $repeatDiscountId = $repeatDiscount->getId(); + $update['repeatDiscounts'][0]['id'] = $repeatDiscountId; + } + } } $this->customerRepository->update([$update], $event->getContext());