src/EventSubscriber/Shop/AddProductToCartAfter/DeliveryPaymentMethodValidationSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Shop\AddProductToCartAfter;
  3. use App\Event\Shop\AddProductToCartAfterEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. /**
  6.  * @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  7.  */
  8. class DeliveryPaymentMethodValidationSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents()
  11.     {
  12.         return [
  13.             AddProductToCartAfterEvent::NAME => [
  14.                 ['checkDeliveryAndPaymentMethod'500],
  15.             ]
  16.         ];
  17.     }
  18.     // Check delivery method and payment method and remove if not available
  19.     public function checkDeliveryAndPaymentMethod(AddProductToCartAfterEvent $event): void
  20.     {
  21.         $cart $event->getCurrentCart();
  22.         $paymentMethod $cart->getPaymentMethod();
  23.         $deliveryMethod $cart->getDeliveryMethod();
  24.         if ($paymentMethod && !($paymentAvailable $cart->isPaymentMethodAvailable($paymentMethod)))
  25.             $cart->setPaymentMethod(null);
  26.         if ($deliveryMethod && !($deliveryAvailable $cart->isDeliveryMethodAvailable($deliveryMethod)))
  27.             $cart->setDeliveryMethod(null);
  28.     }
  29. }