Skip to content

Commit 25c2442

Browse files
authored
Merge pull request #33 from leanormandon/main
Add EventListener to disallow price change
2 parents cca83c9 + cc4faed commit 25c2442

7 files changed

+87
-7
lines changed

Config/module.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<descriptive locale="fr_FR">
88
<title>Famille de clients et prix d'achat</title>
99
</descriptive>
10-
<version>3.1.12</version>
10+
<version>3.2.0</version>
1111
<author>
1212
<name>Guillaume Barral / Etienne Perriere / Vincent Lopes</name>
1313
<email>gbarral@openstudio.fr / eperriere@openstudio.fr / vlopes@openstudio.fr</email>

Event/CustomerFamilyEvents.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ class CustomerFamilyEvents
2222
const CUSTOMER_FAMILY_CREATE = "action.admin.customer.family.create";
2323
const CUSTOMER_FAMILY_UPDATE = "action.admin.customer.family.update";
2424
const CUSTOMER_FAMILY_DELETE = "action.admin.customer.family.delete";
25+
const CUSTOMER_FAMILY_PRICE_CHANGE = "action.admin.customer.family.price_change";
2526
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CustomerFamily\Event;
4+
5+
use CustomerFamily\Model\CustomerFamily;
6+
use Thelia\Core\Event\ActionEvent;
7+
8+
class CustomerFamilyPriceChangeEvent extends ActionEvent
9+
{
10+
public function __construct(
11+
private readonly ?CustomerFamily $customerFamily = null,
12+
private bool $allowPriceChange = true
13+
) { }
14+
15+
public function getCustomerFamily(): ?CustomerFamily
16+
{
17+
return $this->customerFamily;
18+
}
19+
20+
public function getAllowPriceChange(): bool
21+
{
22+
return $this->allowPriceChange;
23+
}
24+
25+
public function setAllowPriceChange($allowPriceChange): bool
26+
{
27+
return $this->allowPriceChange = $allowPriceChange;
28+
}
29+
}

EventListener/CustomerFamilyCartListener.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace CustomerFamily\EventListener;
44

5+
use CustomerFamily\Event\CustomerFamilyEvents;
6+
use CustomerFamily\Event\CustomerFamilyPriceChangeEvent;
57
use CustomerFamily\Service\CustomerFamilyService;
8+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
69
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
710
use Thelia\Core\Event\Cart\CartEvent;
811
use Thelia\Core\Event\TheliaEvents;
@@ -17,9 +20,12 @@ class CustomerFamilyCartListener implements EventSubscriberInterface
1720
{
1821
protected $customerFamilyService;
1922

20-
public function __construct(CustomerFamilyService $customerFamilyService)
23+
private EventDispatcherInterface $dispatcher;
24+
25+
public function __construct(CustomerFamilyService $customerFamilyService, EventDispatcherInterface $dispatcher)
2126
{
2227
$this->customerFamilyService = $customerFamilyService;
28+
$this->dispatcher = $dispatcher;
2329
}
2430

2531
/**
@@ -41,6 +47,13 @@ public static function getSubscribedEvents()
4147
*/
4248
public function addCartItem(CartEvent $cartEvent)
4349
{
50+
$event = new CustomerFamilyPriceChangeEvent();
51+
$this->dispatcher->dispatch($event, CustomerFamilyEvents::CUSTOMER_FAMILY_PRICE_CHANGE);
52+
53+
if (!$event->getAllowPriceChange()) {
54+
return;
55+
}
56+
4457
$cartItem = $cartEvent->getCartItem();
4558
$this->customerFamilyService->setCustomerFamilyPriceToCartItem($cartItem);
4659

EventListener/CustomerFamilyCustomerConnectionListener.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CustomerFamily\EventListener;
44

5+
use CustomerFamily\Event\CustomerFamilyEvents;
6+
use CustomerFamily\Event\CustomerFamilyPriceChangeEvent;
57
use CustomerFamily\Service\CustomerFamilyService;
68
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
79
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -22,7 +24,7 @@ class CustomerFamilyCustomerConnectionListener implements EventSubscriberInterfa
2224
{
2325
protected $requestStack;
2426
protected $customerFamilyService;
25-
protected $dispatcher;
27+
protected EventDispatcherInterface $dispatcher;
2628

2729
public function __construct(
2830
RequestStack $requestStack,
@@ -50,6 +52,13 @@ public static function getSubscribedEvents()
5052

5153
public function refreshCartItemPrices(ActionEvent $event)
5254
{
55+
$event = new CustomerFamilyPriceChangeEvent();
56+
$this->dispatcher->dispatch($event, CustomerFamilyEvents::CUSTOMER_FAMILY_PRICE_CHANGE);
57+
58+
if (!$event->getAllowPriceChange()) {
59+
return;
60+
}
61+
5362
$cart = $this->requestStack->getCurrentRequest()->getSession()->getSessionCart($this->dispatcher);
5463

5564
foreach ($cart->getCartItems() as $cartItem) {

EventListener/PseByProductListener.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace CustomerFamily\EventListener;
44

5+
use CustomerFamily\Event\CustomerFamilyEvents;
6+
use CustomerFamily\Event\CustomerFamilyPriceChangeEvent;
57
use CustomerFamily\Service\CustomerFamilyService;
8+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
69
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
710
use Thelia\Core\Security\SecurityContext;
811
use TheliaSmarty\Events\PseByProductEvent;
@@ -12,18 +15,28 @@ class PseByProductListener implements EventSubscriberInterface
1215
protected $customerFamilyService;
1316

1417
protected $securityContext;
18+
protected EventDispatcherInterface $dispatcher;
1519

1620
public function __construct(
1721
CustomerFamilyService $customerFamilyService,
18-
SecurityContext $securityContext
22+
SecurityContext $securityContext,
23+
EventDispatcherInterface $dispatcher
1924
)
2025
{
2126
$this->customerFamilyService = $customerFamilyService;
2227
$this->securityContext = $securityContext;
28+
$this->dispatcher = $dispatcher;
2329
}
2430

2531
public function updatePriceInPseByProduct(PseByProductEvent $event)
2632
{
33+
34+
$event = new CustomerFamilyPriceChangeEvent();
35+
$this->dispatcher->dispatch($event, CustomerFamilyEvents::CUSTOMER_FAMILY_PRICE_CHANGE);
36+
37+
if (!$event->getAllowPriceChange()) {
38+
return;
39+
}
2740
$pse = $event->getProductSaleElements();
2841
$prices = $this->customerFamilyService->calculateCustomerFamilyPsePrice($pse);
2942

@@ -51,4 +64,4 @@ public static function getSubscribedEvents()
5164
PseByProductEvent::class => ['updatePriceInPseByProduct', 128]
5265
];
5366
}
54-
}
67+
}

LoopExtend/CustomerFamilyPriceListener.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace CustomerFamily\LoopExtend;
44

55
use CustomerFamily\CustomerFamily;
6+
use CustomerFamily\Event\CustomerFamilyEvents;
7+
use CustomerFamily\Event\CustomerFamilyPriceChangeEvent;
68
use CustomerFamily\Model\Map\CustomerFamilyProductPriceTableMap;
79
use CustomerFamily\Model\Map\ProductPurchasePriceTableMap;
810
use CustomerFamily\Service\CustomerFamilyService;
911
use Propel\Runtime\ActiveQuery\Criteria;
1012
use Propel\Runtime\ActiveQuery\Join;
1113
use Propel\Runtime\ActiveQuery\ModelCriteria;
1214
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1316
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1417
use Thelia\Core\Event\Loop\LoopExtendsBuildModelCriteriaEvent;
1518
use Thelia\Core\Event\Loop\LoopExtendsParseResultsEvent;
@@ -29,12 +32,18 @@ class CustomerFamilyPriceListener implements EventSubscriberInterface
2932
protected $securityContext;
3033
protected $taxEngine;
3134
protected $customerFamilyService;
35+
protected EventDispatcherInterface $dispatcher;
3236

33-
public function __construct(SecurityContext $securityContext, TaxEngine $taxEngine, CustomerFamilyService $customerFamilyService)
34-
{
37+
public function __construct(
38+
SecurityContext $securityContext,
39+
TaxEngine $taxEngine,
40+
CustomerFamilyService $customerFamilyService,
41+
EventDispatcherInterface $dispatcher
42+
) {
3543
$this->securityContext = $securityContext;
3644
$this->taxEngine = $taxEngine;
3745
$this->customerFamilyService = $customerFamilyService;
46+
$this->dispatcher = $dispatcher;
3847
}
3948

4049
public static function getSubscribedEvents()
@@ -102,6 +111,12 @@ public function extendProductModelCriteria(LoopExtendsBuildModelCriteriaEvent $e
102111

103112
public function extendProductParseResult(LoopExtendsParseResultsEvent $event)
104113
{
114+
$event = new CustomerFamilyPriceChangeEvent();
115+
$this->dispatcher->dispatch($event, CustomerFamilyEvents::CUSTOMER_FAMILY_PRICE_CHANGE);
116+
117+
if (!$event->getAllowPriceChange()) {
118+
return;
119+
}
105120
if ($event->getLoop()->getBackendContext()) {
106121
return;
107122
}

0 commit comments

Comments
 (0)