/** * Method subscribed to PreCartLoad event. * * Iterate over all automatic Coupons and check if they apply. * If any applies, it will be added to the Cart * * @param CartInterface $cart Cart */ public function tryAutomaticCoupons(CartInterface $cart) { if ($cart->getCartLines()->isEmpty()) { return; } $automaticCoupons = $this->getNonAppliedAutomaticCoupons($cart); foreach ($automaticCoupons as $coupon) { try { $this->cartCouponManager->addCoupon($cart, $coupon); } catch (AbstractCouponException $e) { // Silently tries next coupon on controlled exception } } }
public function testStackableCouponCalculatedAmount() { /** * Stackable Coupons from fixtures * * id 3: 12 % discount * id 4: 2 USD discount * * See CouponData fixtures for details * * @var Coupon $stacableCouponPercent * @var Coupon $stackableCouponAmount */ $stackableCouponPercent = $this->find('coupon', 3); $stackableCouponAmount = $this->find('coupon', 4); $this->cartCouponManager->addCoupon($this->cart, $stackableCouponPercent->setEnabled(true)); $this->cartCouponManager->addCoupon($this->cart, $stackableCouponAmount->setEnabled(true)); /** * Dispatching cart.load events will recalculate * cart coupon amount */ $this->cartEventDispatcher->dispatchCartLoadEvents($this->cart); $appliedCouponsAmount = $this->cart->getCouponAmount()->getAmount(); $this->assertEquals(560, $appliedCouponsAmount); }
/** * Method subscribed to PreCartLoad event * * Iterate over all automatic Coupons and check if they apply. * If any applies, it will be added to the Cart * * @param CartOnLoadEvent $event Event * * @return null */ public function tryAutomaticCoupons(CartOnLoadEvent $event) { $cart = $event->getCart(); if ($cart->getCartLines()->isEmpty()) { return null; } /** * @var CouponInterface[] $automaticCoupons */ $automaticCoupons = $this->couponRepository->findBy(['enforcement' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC]); foreach ($automaticCoupons as $coupon) { try { $this->cartCouponManager->addCoupon($cart, $coupon); } catch (AbstractCouponException $e) { // Silently tries next coupon on controlled exception } } }