<?php
namespace App\EventSubscriber\Shop\AddProductToCartAfter;
use App\Event\Shop\AddProductToCartAfterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
*/
class DeliveryPaymentMethodValidationSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
AddProductToCartAfterEvent::NAME => [
['checkDeliveryAndPaymentMethod', 500],
]
];
}
// Check delivery method and payment method and remove if not available
public function checkDeliveryAndPaymentMethod(AddProductToCartAfterEvent $event): void
{
$cart = $event->getCurrentCart();
$paymentMethod = $cart->getPaymentMethod();
$deliveryMethod = $cart->getDeliveryMethod();
if ($paymentMethod && !($paymentAvailable = $cart->isPaymentMethodAvailable($paymentMethod)))
$cart->setPaymentMethod(null);
if ($deliveryMethod && !($deliveryAvailable = $cart->isDeliveryMethodAvailable($deliveryMethod)))
$cart->setDeliveryMethod(null);
}
}