-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MOL-1297: Fix payment method cache in production
- Loading branch information
Vitalij Mik
committed
Feb 15, 2024
1 parent
1914194
commit c6598c6
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Kiener\MolliePayments\Subscriber; | ||
|
||
use Shopware\Core\Checkout\Cart\SalesChannel\CartService; | ||
use Shopware\Core\Checkout\Payment\Event\PaymentMethodRouteCacheKeyEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class PaymentMethodRouteCacheKeySubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var CartService | ||
*/ | ||
private $cartService; | ||
|
||
public function __construct(CartService $cartService) | ||
{ | ||
$this->cartService = $cartService; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PaymentMethodRouteCacheKeyEvent::class => 'onInvalidateCache' | ||
]; | ||
} | ||
|
||
/** | ||
* shopware caches the payment methods only based on criteria. we add the cart price, currency and billing address id | ||
* @param PaymentMethodRouteCacheKeyEvent $event | ||
* @return void | ||
*/ | ||
public function onInvalidateCache(PaymentMethodRouteCacheKeyEvent $event) | ||
{ | ||
$context = $event->getContext(); | ||
|
||
$customer = $context->getCustomer(); | ||
if ($customer === null) { | ||
return; | ||
} | ||
$billingAddress = $customer->getActiveBillingAddress(); | ||
$billingAddressId = ''; | ||
if ($billingAddress !== null) { | ||
$billingAddressId = $billingAddress->getId(); | ||
} | ||
|
||
$cart = $this->cartService->getCart($context->getToken(), $context); | ||
|
||
$cacheParts = $event->getParts(); | ||
|
||
$cacheParts[] = md5(implode([ | ||
$cart->getPrice()->getTotalPrice(), | ||
$context->getCurrency()->getIsoCode(), | ||
$billingAddressId | ||
])); | ||
|
||
$event->setParts($cacheParts); | ||
} | ||
} |