/** * Check for the rules required by the coupon * * @param CartCouponOnCheckEvent $event Event * * @throws CouponRulesNotValidateException */ public function checkCoupon(CartCouponOnCheckEvent $event) { $isValid = $this->cartCouponRuleManager->checkCouponValidity($event->getCart(), $event->getCoupon()); if (!$isValid) { throw new CouponRulesNotValidateException(); } }
/** * Applies Coupon in Cart, and flushes it * * @param CartCouponOnApplyEvent $event Event * * @throws CouponRulesNotValidateException Invalid rules */ public function onCartCouponApply(CartCouponOnApplyEvent $event) { $cart = $event->getCart(); $coupon = $event->getCoupon(); if (!$this->cartCouponRuleManager->checkCouponValidity($cart, $coupon)) { $this->cartCouponEventDispatcher->dispatchCartCouponOnRejectedEvent($cart, $coupon); $event->stopPropagation(); } }
/** * Method subscribed to PreCartLoad event * * Checks if all Coupons applied to current cart are still valid. * If are not, they will be deleted from the Cart and new Event typeof * CartCouponOnRejected will be dispatched * * @param CartPreLoadEvent $cartPreLoadEvent Event */ public function onCartPreLoadCoupons(CartPreLoadEvent $cartPreLoadEvent) { $cart = $cartPreLoadEvent->getCart(); $cartCoupons = $this->cartCouponManager->getCartCoupons($cart); /** * @var CartCouponInterface $cartCoupon */ foreach ($cartCoupons as $cartCoupon) { $coupon = $cartCoupon->getCoupon(); if (!$this->cartCouponRuleManager->checkCouponValidity($cart, $coupon)) { $this->cartCouponManager->removeCoupon($cart, $coupon); $this->cartCouponEventDispatcher->dispatchCartCouponOnRejectedEvent($cart, $coupon); } } }