/** * Check if this coupon is already applied to the cart. * * @param CartInterface $cart Cart * @param CouponInterface $coupon Coupon * * @throws CouponAlreadyAppliedException Coupon already applied */ public function validateDuplicatedCoupon(CartInterface $cart, CouponInterface $coupon) { $cartCoupon = $this->cartCouponRepository->findOneBy(['cart' => $cart, 'coupon' => $coupon]); if (null !== $cartCoupon) { throw new CouponAlreadyAppliedException(); } }
/** * Check if this coupon is already applied to the cart * * @param CartCouponOnApplyEvent $event Event * * @throws CouponAlreadyAppliedException */ public function checkDuplicates(CartCouponOnApplyEvent $event) { $cartCoupon = $this->cartCouponRepository->findOneBy(['cart' => $event->getCart(), 'coupon' => $event->getCoupon()]); if (null !== $cartCoupon) { throw new CouponAlreadyAppliedException(); } }
/** * 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(); } } }
/** * 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(); }
/** * Get cart coupon objects. * * @param CartInterface $cart Cart * * @return CouponInterface[] Coupons */ public function getCoupons(CartInterface $cart) { /** * If Cart id is null means that this cart has been generated from * scratch. This also means that it cannot have any Coupon associated. * If is this case, we avoid this lookup. */ if ($cart->getId() === null) { return []; } return $this->cartCouponRepository->findCouponsByCart($cart); }
/** * Create new CartCouponManager instance * * @return CartCouponManager */ private function createCartCouponManagerInstance() { return new CartCouponManager($this->cartCouponEventDispatcher->reveal(), $this->couponRepository->reveal(), $this->cartCouponDirector->reveal(), $this->cartCouponRepository->reveal()); }