| @@ -4,7 +4,9 @@ | |||||
| namespace Atl\DiscountExtension\Subscriber; | namespace Atl\DiscountExtension\Subscriber; | ||||
| use Atl\DiscountExtension\Core\System\RepeatDiscount\RepeatDiscountCollection; | |||||
| use Atl\DiscountExtension\Core\System\RepeatDiscount\RepeatDiscountEntity; | use Atl\DiscountExtension\Core\System\RepeatDiscount\RepeatDiscountEntity; | ||||
| use Psr\Log\LoggerInterface; | |||||
| use Shopware\Core\Checkout\Customer\CustomerEntity; | use Shopware\Core\Checkout\Customer\CustomerEntity; | ||||
| use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection; | use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection; | ||||
| use Shopware\Core\Checkout\Order\OrderEntity; | 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\StateMachine\Event\StateMachineTransitionEvent; | ||||
| use Shopware\Core\System\SystemConfig\SystemConfigService; | use Shopware\Core\System\SystemConfig\SystemConfigService; | ||||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||||
| use Symfony\Component\Serializer\Serializer; | |||||
| class StateMachineTransitionSubscriber implements EventSubscriberInterface | class StateMachineTransitionSubscriber implements EventSubscriberInterface | ||||
| { | { | ||||
| @@ -77,7 +80,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| $orderId = $event->getEntityId(); | $orderId = $event->getEntityId(); | ||||
| $order = $this->getOrder($orderId, $event); | $order = $this->getOrder($orderId, $event); | ||||
| $salesChannelId = $order->getSalesChannelId(); | $salesChannelId = $order->getSalesChannelId(); | ||||
| // In case no order could be found - return | // In case no order could be found - return | ||||
| if (empty($order)) { | if (empty($order)) { | ||||
| return; | return; | ||||
| @@ -85,14 +88,14 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| $minAmountDiscountableLineItems = $this->systemConfigService->get('AtlDiscountExtension.config.minAmountDiscountableLineItems', $salesChannelId); | $minAmountDiscountableLineItems = $this->systemConfigService->get('AtlDiscountExtension.config.minAmountDiscountableLineItems', $salesChannelId); | ||||
| $amountDiscountableLineItems = $this->getAmountDiscountableLineItems($order->getLineItems(), $minAmountDiscountableLineItems, $salesChannelId, $event); | $amountDiscountableLineItems = $this->getAmountDiscountableLineItems($order->getLineItems(), $minAmountDiscountableLineItems, $salesChannelId, $event); | ||||
| // In case it is not enough - return | // In case it is not enough - return | ||||
| if ($amountDiscountableLineItems < $minAmountDiscountableLineItems) { | if ($amountDiscountableLineItems < $minAmountDiscountableLineItems) { | ||||
| return; | return; | ||||
| } | } | ||||
| $customer = $order->getOrderCustomer()->getCustomer(); | $customer = $order->getOrderCustomer()->getCustomer(); | ||||
| // Tag the customer and trace last order date in 'spwn_repeat_discount' table | // Tag the customer and trace last order date in 'spwn_repeat_discount' table | ||||
| $this->tagCustomer($customer, $salesChannelId, $event); | $this->tagCustomer($customer, $salesChannelId, $event); | ||||
| } | } | ||||
| @@ -107,7 +110,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| $criteria = new Criteria(); | $criteria = new Criteria(); | ||||
| $criteria->addFilter(new EqualsFilter('id', $orderId)); | $criteria->addFilter(new EqualsFilter('id', $orderId)); | ||||
| $criteria->addAssociation('lineItems'); | $criteria->addAssociation('lineItems'); | ||||
| $criteria->addAssociation('orderCustomer.customer'); | |||||
| $criteria->addAssociation('orderCustomer.customer.repeatDiscounts'); | |||||
| return $this->orderRepository->search($criteria, $event->getContext())->first(); | return $this->orderRepository->search($criteria, $event->getContext())->first(); | ||||
| } | } | ||||
| @@ -133,15 +136,20 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| if ($customFieldName === '') { | if ($customFieldName === '') { | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| $amountDiscountableLineItems = 0; | $amountDiscountableLineItems = 0; | ||||
| foreach($lineItems as $lineItem) { | foreach($lineItems as $lineItem) { | ||||
| // If no product lineItem - continue | |||||
| if (empty($lineItem->getPayload()['customFields'])) { | if (empty($lineItem->getPayload()['customFields'])) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $customFields = $lineItem->getPayload()['customFields']; | $customFields = $lineItem->getPayload()['customFields']; | ||||
| // Checks if the custom field exists and is set to true | // 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(); | $amountDiscountableLineItems += $lineItem->getQuantity(); | ||||
| } | } | ||||
| @@ -150,6 +158,7 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| return $amountDiscountableLineItems; | return $amountDiscountableLineItems; | ||||
| } | } | ||||
| @@ -197,19 +206,28 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface | |||||
| 'id' => $tagId | '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 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()); | $this->customerRepository->update([$update], $event->getContext()); | ||||