From c6598c67121eb11bee16e2bf814d534ae9e1b716 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 15 Feb 2024 12:45:54 +0100 Subject: [PATCH 1/3] MOL-1297: Fix payment method cache in production --- src/Resources/config/services/subscriber.xml | 7 +++ .../PaymentMethodRouteCacheKeySubscriber.php | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php diff --git a/src/Resources/config/services/subscriber.xml b/src/Resources/config/services/subscriber.xml index be985f435..7c7b36774 100644 --- a/src/Resources/config/services/subscriber.xml +++ b/src/Resources/config/services/subscriber.xml @@ -63,5 +63,12 @@ + + + + + + + diff --git a/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php b/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php new file mode 100644 index 000000000..0195f93ac --- /dev/null +++ b/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php @@ -0,0 +1,60 @@ +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); + } +} From 8666f5da26f37a521c9714dc89609ecdc0a068f2 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Fri, 16 Feb 2024 10:02:28 +0100 Subject: [PATCH 2/3] MOL-1297: refactor --- .../Cache/CachedPaymentMethodRoute64.php | 46 ++++++++++++-- src/Resources/config/services/subscriber.xml | 6 -- .../PaymentMethodRouteCacheKeySubscriber.php | 60 ------------------- 3 files changed, 41 insertions(+), 71 deletions(-) delete mode 100644 src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php diff --git a/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php b/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php index c353bc8f1..7618e0368 100644 --- a/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php +++ b/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php @@ -8,6 +8,7 @@ use Shopware\Core\Checkout\Cart\Cart; use Shopware\Core\Checkout\Cart\SalesChannel\CartService; use Shopware\Core\Checkout\Payment\Event\PaymentMethodRouteCacheKeyEvent; +use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class CachedPaymentMethodRoute64 implements EventSubscriberInterface @@ -66,11 +67,15 @@ public function onGenerateCacheKey(PaymentMethodRouteCacheKeyEvent $event): void $event->getContext()->setRuleIds($originalRuleIds); $parts = $event->getParts(); - - $parts = $this->addVoucherKey($cart, $parts); - $parts = $this->addMollieLimitsKey($parts); - $parts = $this->addSubscriptionKey($cart, $parts); - + $cacheParts = []; + $cacheParts = $this->addVoucherKey($cart, $cacheParts); + $cacheParts = $this->addMollieLimitsKey($cacheParts); + $cacheParts = $this->addSubscriptionKey($cart, $cacheParts); + $cacheParts = $this->addCartAmountKey($cart, $cacheParts); + $cacheParts = $this->addCurrencyCodeKey($event->getContext(), $cacheParts); + $cacheParts = $this->addBillingAddressKey($event->getContext(), $cacheParts); + + $parts[] = md5(implode('-', $cacheParts)); $event->setParts($parts); } @@ -145,4 +150,35 @@ private function isSubscriptionCart(Cart $cart): bool return false; } + + private function addCartAmountKey(Cart $cart, array $cacheParts): array + { + $cacheParts[] = $cart->getPrice()->getTotalPrice(); + return $cacheParts; + } + + private function addCurrencyCodeKey(SalesChannelContext $context, array $cacheParts) + { + $cacheParts[] = $context->getCurrency()->getIsoCode(); + return $cacheParts; + } + + private function addBillingAddressKey(SalesChannelContext $context, array $cacheParts) + { + $customer = $context->getCustomer(); + + if ($customer === null) { + return $cacheParts; + } + + $billingAddress = $customer->getActiveBillingAddress(); + + if ($billingAddress === null) { + return $cacheParts; + } + + $cacheParts[]=$billingAddress->getId(); + + return $cacheParts; + } } diff --git a/src/Resources/config/services/subscriber.xml b/src/Resources/config/services/subscriber.xml index 7c7b36774..ba25c3490 100644 --- a/src/Resources/config/services/subscriber.xml +++ b/src/Resources/config/services/subscriber.xml @@ -64,11 +64,5 @@ - - - - - - diff --git a/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php b/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php deleted file mode 100644 index 0195f93ac..000000000 --- a/src/Subscriber/PaymentMethodRouteCacheKeySubscriber.php +++ /dev/null @@ -1,60 +0,0 @@ -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); - } -} From cff11b0ccacc90f9eae5e034b8a0b14558f47260 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Fri, 16 Feb 2024 10:07:41 +0100 Subject: [PATCH 3/3] MOL-1297: codestyle fix --- .../Cache/CachedPaymentMethodRoute64.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php b/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php index 7618e0368..d2ee4bbe5 100644 --- a/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php +++ b/src/Compatibility/Storefront/Route/PaymentMethodRoute/Cache/CachedPaymentMethodRoute64.php @@ -151,19 +151,34 @@ private function isSubscriptionCart(Cart $cart): bool return false; } + /** + * @param Cart $cart + * @param array $cacheParts + * @return array + */ private function addCartAmountKey(Cart $cart, array $cacheParts): array { $cacheParts[] = $cart->getPrice()->getTotalPrice(); return $cacheParts; } - private function addCurrencyCodeKey(SalesChannelContext $context, array $cacheParts) + /** + * @param SalesChannelContext $context + * @param array $cacheParts + * @return array + */ + private function addCurrencyCodeKey(SalesChannelContext $context, array $cacheParts):array { $cacheParts[] = $context->getCurrency()->getIsoCode(); return $cacheParts; } - private function addBillingAddressKey(SalesChannelContext $context, array $cacheParts) + /** + * @param SalesChannelContext $context + * @param array $cacheParts + * @return array + */ + private function addBillingAddressKey(SalesChannelContext $context, array $cacheParts):array { $customer = $context->getCustomer();