/** * 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(); } }
/** * 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 CartInterface $cart Cart */ public function validateCartCoupons(CartInterface $cart) { $cartCoupons = $this->cartCouponManager->getCartCoupons($cart); foreach ($cartCoupons as $cartCoupon) { $coupon = $cartCoupon->getCoupon(); try { $this->cartCouponEventDispatcher->dispatchCartCouponOnCheckEvent($cart, $coupon); } catch (AbstractCouponException $exception) { $this->cartCouponManager->removeCoupon($cart, $coupon); $this->cartCouponEventDispatcher->dispatchCartCouponOnRejectedEvent($cart, $coupon); } } }
/** * Removes a Coupon from a Cart, and recalculates Cart Totals. * * @param CartInterface $cart Cart * @param CouponInterface $coupon The coupon to remove * * @return bool Coupon has been removed from cart */ public function removeCoupon(CartInterface $cart, CouponInterface $coupon) { $cartCoupons = $this->cartCouponDirector->findBy(['cart' => $cart, 'coupon' => $coupon]); if (empty($cartCoupons)) { return false; } foreach ($cartCoupons as $cartCoupon) { $this->cartCouponEventDispatcher->dispatchCartCouponOnRemoveEvent($cartCoupon); } return true; }
/** * Method subscribed to CartLoad 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 CartOnLoadEvent $event Event */ public function refreshCartCoupons(CartOnLoadEvent $event) { $cart = $event->getCart(); $cartCoupons = $this->cartCouponManager->getCartCoupons($cart); foreach ($cartCoupons as $cartCoupon) { $coupon = $cartCoupon->getCoupon(); try { $this->cartCouponEventDispatcher->dispatchCartCouponOnCheckEvent($cart, $coupon); } catch (AbstractCouponException $exception) { $this->cartCouponManager->removeCoupon($cart, $coupon); $this->cartCouponEventDispatcher->dispatchCartCouponOnRejectedEvent($cart, $coupon); } } }
/** * 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); } } }
/** * Create new CartCouponManager instance * * @return CartCouponManager */ private function createCartCouponManagerInstance() { return new CartCouponManager($this->cartCouponEventDispatcher->reveal(), $this->couponRepository->reveal(), $this->cartCouponDirector->reveal(), $this->cartCouponRepository->reveal()); }
/** * Checks if a Coupon is applicable to a Cart * * @param CartCouponOnApplyEvent $event Event */ public function validateCoupon(CartCouponOnApplyEvent $event) { $this->cartCouponDispatcher->dispatchCartCouponOnCheckEvent($event->getCart(), $event->getCoupon()); }