Skip to content

Commit

Permalink
MOL-1297: Fix payment method cache in production (#706)
Browse files Browse the repository at this point in the history
* MOL-1297: Fix payment method cache in production

* MOL-1297: refactor

* MOL-1297: codestyle fix

---------

Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Feb 16, 2024
1 parent 4ecd79d commit 20538c4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -145,4 +150,50 @@ private function isSubscriptionCart(Cart $cart): bool

return false;
}

/**
* @param Cart $cart
* @param array<mixed> $cacheParts
* @return array<mixed>
*/
private function addCartAmountKey(Cart $cart, array $cacheParts): array
{
$cacheParts[] = $cart->getPrice()->getTotalPrice();
return $cacheParts;
}

/**
* @param SalesChannelContext $context
* @param array<mixed> $cacheParts
* @return array<mixed>
*/
private function addCurrencyCodeKey(SalesChannelContext $context, array $cacheParts):array
{
$cacheParts[] = $context->getCurrency()->getIsoCode();
return $cacheParts;
}

/**
* @param SalesChannelContext $context
* @param array<mixed> $cacheParts
* @return array<mixed>
*/
private function addBillingAddressKey(SalesChannelContext $context, array $cacheParts):array
{
$customer = $context->getCustomer();

if ($customer === null) {
return $cacheParts;
}

$billingAddress = $customer->getActiveBillingAddress();

if ($billingAddress === null) {
return $cacheParts;
}

$cacheParts[]=$billingAddress->getId();

return $cacheParts;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
<argument type="service" id="mollie_payments.logger"/>
<tag name="kernel.event_subscriber"/>
</service>

</services>
</container>

0 comments on commit 20538c4

Please sign in to comment.