I am trying to hide certain payment method (a custom one, let's call it MyPaygate
) on checkou/confirm page if certain currency is selected. I have tried several different aproaches, but nothing worked. I am trying to use Subscriber for an event CheckoutConfirmPageLoadedEvent
but it doesn't seam to be ever triggered.
EDIT: after Uwe's suggestion I came up with this:
src/Core/Checkout/Payment/SalesChannel/DecoratedPaymentMethodRoute.php
<?php declare(strict_types=1);
namespace Test\Paygate\Core\Checkout\Payment\SalesChannel;
use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRoute;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRouteResponse;
class DecoratedPaymentMethodRoute extends AbstractPaymentMethodRoute
{
private $originalService;
public function __construct(PaymentMethodRoute $originalService)
{
$this->originalService = $originalService;
}
public function load(Request $request, SalesChannelContext $context, Criteria $criteria): PaymentMethodRouteResponse
{
$response = $this->originalService->load($request, $context, $criteria);
$paymentMethods = $response->getPaymentMethods()->filter(function ($paymentMethod) use ($context) {
if ($context->getCurrency()->getIsoCode() === 'EUR' && $paymentMethod->getHandlerIdentifier() === 'TestPaygate') {
return false;
}
return true;
});
$response->setPaymentMethods($paymentMethods);
return $response;
}
public function getDecorated(): PaymentMethodRoute
{
return $this->originalService;
}
}
Relevant part of services.xml:
<service id="Test\Paygate\Core\Checkout\Payment\SalesChannel\DecoratedPaymentMethodRoute" class="Test\Paygate\Core\Checkout\Payment\SalesChannel\DecoratedPaymentMethodRoute">
<argument type="service" id="Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRoute" />
<tag name="shopware.sales_channel.route_decorator"/>
</service>
It still doesn't work. I have tried to log something from load & construct methods and nothing happens, so it seams like the methods are not even triggered.
I ended up using following code for
CheckoutConfirmPageLoadedEvent
: