|
|
@@ -76,14 +76,15 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
|
|
$orderDeliveryId = $event->getEntityId(); |
|
|
$orderDeliveryId = $event->getEntityId(); |
|
|
$orderDelivery = $this->getOrderDelivery($orderDeliveryId, $event); |
|
|
$orderDelivery = $this->getOrderDelivery($orderDeliveryId, $event); |
|
|
|
|
|
$salesChannelId = $orderDelivery->getOrder()->getSalesChannelId(); |
|
|
|
|
|
|
|
|
// In case no order delivery could be found - return |
|
|
// In case no order delivery could be found - return |
|
|
if (empty($orderDelivery)) { |
|
|
if (empty($orderDelivery)) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$amountDiscountableLineItems = $this->getAmountDiscountableLineItems($orderDelivery->getPositions(), $event); |
|
|
|
|
|
$minAmountDiscountableLineItems = $this->systemConfigService->get('AtlDiscountExtension.config.minAmountDiscountableLineItems'); |
|
|
|
|
|
|
|
|
$minAmountDiscountableLineItems = $this->systemConfigService->get('AtlDiscountExtension.config.minAmountDiscountableLineItems', $salesChannelId); |
|
|
|
|
|
$amountDiscountableLineItems = $this->getAmountDiscountableLineItems($orderDelivery->getPositions(), $minAmountDiscountableLineItems, $salesChannelId, $event); |
|
|
|
|
|
|
|
|
if ($amountDiscountableLineItems < $minAmountDiscountableLineItems) { |
|
|
if ($amountDiscountableLineItems < $minAmountDiscountableLineItems) { |
|
|
return; |
|
|
return; |
|
|
@@ -91,8 +92,8 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
|
|
$customer = $orderDelivery->getOrder()->getOrderCustomer()->getCustomer(); |
|
|
$customer = $orderDelivery->getOrder()->getOrderCustomer()->getCustomer(); |
|
|
|
|
|
|
|
|
// Tag the customer and trace timestamp in 'spwn_repeat_discount' table |
|
|
|
|
|
$this->tagCustomer($customer, $event); |
|
|
|
|
|
|
|
|
// Tag the customer and trace last order date in 'spwn_repeat_discount' table |
|
|
|
|
|
$this->tagCustomer($customer, $salesChannelId, $event); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@@ -112,11 +113,20 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param OrderDeliveryPositionCollection $positions |
|
|
* @param OrderDeliveryPositionCollection $positions |
|
|
|
|
|
* @param int $minAmountDiscountableLineItems |
|
|
|
|
|
* @param string $salesChannelId |
|
|
|
|
|
* @param StateMachineTransitionEvent $event |
|
|
* @return int |
|
|
* @return int |
|
|
*/ |
|
|
*/ |
|
|
private function getAmountDiscountableLineItems(OrderDeliveryPositionCollection $positions, StateMachineTransitionEvent $event): int |
|
|
|
|
|
|
|
|
private function getAmountDiscountableLineItems |
|
|
|
|
|
( |
|
|
|
|
|
OrderDeliveryPositionCollection $positions, |
|
|
|
|
|
int $minAmountDiscountableLineItems, |
|
|
|
|
|
string $salesChannelId, |
|
|
|
|
|
StateMachineTransitionEvent $event |
|
|
|
|
|
): int |
|
|
{ |
|
|
{ |
|
|
$customFieldName = $this->getCustomFieldName($event); |
|
|
|
|
|
|
|
|
$customFieldName = $this->getCustomFieldName($event, $salesChannelId); |
|
|
|
|
|
|
|
|
// In case no custom field was assigned in the plugin config - return |
|
|
// In case no custom field was assigned in the plugin config - return |
|
|
if ($customFieldName === '') { |
|
|
if ($customFieldName === '') { |
|
|
@@ -127,9 +137,14 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
foreach($positions as $position) { |
|
|
foreach($positions as $position) { |
|
|
$customFields = $position->getOrderLineItem()->getPayload()['customFields']; |
|
|
$customFields = $position->getOrderLineItem()->getPayload()['customFields']; |
|
|
|
|
|
|
|
|
// Checks if the custom field is set to true |
|
|
|
|
|
if ($customFields[$customFieldName] === true) { |
|
|
|
|
|
$amountDiscountableLineItems++; |
|
|
|
|
|
|
|
|
// Checks if the custom field exists and is set to true |
|
|
|
|
|
if (!empty($customFields[$customFieldName]) && $customFields[$customFieldName] === true) { |
|
|
|
|
|
$amountDiscountableLineItems += $position->getQuantity(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Break if min amount is already reached |
|
|
|
|
|
if ($amountDiscountableLineItems >= $minAmountDiscountableLineItems) { |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -138,18 +153,19 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param StateMachineTransitionEvent $event |
|
|
* @param StateMachineTransitionEvent $event |
|
|
|
|
|
* @param string $salesChannelId |
|
|
* @return string |
|
|
* @return string |
|
|
*/ |
|
|
*/ |
|
|
private function getCustomFieldName(StateMachineTransitionEvent $event): string |
|
|
|
|
|
|
|
|
private function getCustomFieldName(StateMachineTransitionEvent $event, string $salesChannelId): string |
|
|
{ |
|
|
{ |
|
|
$customFieldId = $this->systemConfigService->get('AtlDiscountExtension.config.customField'); |
|
|
|
|
|
|
|
|
$customFieldId = $this->systemConfigService->get('AtlDiscountExtension.config.customField', $salesChannelId); |
|
|
|
|
|
|
|
|
$criteria = new Criteria(); |
|
|
$criteria = new Criteria(); |
|
|
$criteria->addFilter(new EqualsFilter('id', $customFieldId)); |
|
|
$criteria->addFilter(new EqualsFilter('id', $customFieldId)); |
|
|
|
|
|
|
|
|
/** @var CustomFieldEntity $customField */ |
|
|
/** @var CustomFieldEntity $customField */ |
|
|
$customField = $this->customFieldRepository->search($criteria, $event->getContext())->first(); |
|
|
$customField = $this->customFieldRepository->search($criteria, $event->getContext())->first(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// In case no custom field was assigned in the plugin config - return |
|
|
// In case no custom field was assigned in the plugin config - return |
|
|
if (empty($customField)) { |
|
|
if (empty($customField)) { |
|
|
return ''; |
|
|
return ''; |
|
|
@@ -160,12 +176,13 @@ class StateMachineTransitionSubscriber implements EventSubscriberInterface |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @param CustomerEntity $customer |
|
|
* @param CustomerEntity $customer |
|
|
|
|
|
* @param string $salesChannelId |
|
|
* @param StateMachineTransitionEvent $event |
|
|
* @param StateMachineTransitionEvent $event |
|
|
* @return void |
|
|
* @return void |
|
|
*/ |
|
|
*/ |
|
|
private function tagCustomer(CustomerEntity $customer, StateMachineTransitionEvent $event): void |
|
|
|
|
|
|
|
|
private function tagCustomer(CustomerEntity $customer, string $salesChannelId, StateMachineTransitionEvent $event): void |
|
|
{ |
|
|
{ |
|
|
$tagId = $this->systemConfigService->get('AtlDiscountExtension.config.tag'); |
|
|
|
|
|
|
|
|
$tagId = $this->systemConfigService->get('AtlDiscountExtension.config.tag', $salesChannelId); |
|
|
|
|
|
|
|
|
if (empty($tagId)) { |
|
|
if (empty($tagId)) { |
|
|
return; |
|
|
return; |
|
|
|