/**
  * Avoid applying a manual coupon if another is being applied
  *
  * @param CartCouponOnApplyEvent $event
  *
  * @throws CouponIncompatibleException
  */
 public function assertJustOneManualCoupon(CartCouponOnApplyEvent $event)
 {
     if (!$this->isManual($event->getCoupon())) {
         return null;
     }
     /**
      * @var CartCouponInterface[] $cartCoupons
      */
     $cartCoupons = $this->cartCouponRepository->findBy(['cart' => $event->getCart()]);
     foreach ($cartCoupons as $cartCoupon) {
         if ($this->isManual($cartCoupon->getCoupon())) {
             throw new CouponIncompatibleException();
         }
     }
 }
Example #2
0
 /**
  * Removes a Coupon from a Cart, and recalculates Cart Totals
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon The coupon to remove
  *
  * @return boolean Coupon has been removed from cart
  */
 public function removeCoupon(CartInterface $cart, CouponInterface $coupon)
 {
     $cartCoupons = $this->cartCouponRepository->findBy(array('cart' => $cart, 'coupon' => $coupon));
     if (empty($cartCoupons)) {
         return false;
     }
     foreach ($cartCoupons as $cartCoupon) {
         $this->cartCouponEventDispatcher->dispatchCartCouponOnRemoveEvent($cartCoupon);
     }
     return true;
 }
 /**
  * Check if this coupon can be applied when other coupons had previously
  * been applied.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @throws CouponNotStackableException Coupon is not stackable
  */
 public function validateStackableCoupon(CartInterface $cart, CouponInterface $coupon)
 {
     $cartCoupons = $this->cartCouponRepository->findBy(['cart' => $cart]);
     /**
      * If there are no previously applied coupons we can skip the check.
      */
     if (0 == count($cartCoupons)) {
         return;
     }
     $appliedCouponsCanBeStacked = array_reduce($cartCoupons, function ($previousCouponsAreStackable, CartCouponInterface $cartCoupon) {
         return $previousCouponsAreStackable && $cartCoupon->getCoupon()->getStackable();
     }, true);
     /**
      * Checked coupon can be stackable and everything that was
      * previously applied is also stackable.
      */
     if ($coupon->getStackable() && $appliedCouponsCanBeStacked) {
         return;
     }
     throw new CouponNotStackableException();
 }