/** * A new Order has been created. * * This method adds Coupon logic in this transformation * * @param CartInterface $cart Cart * @param OrderInterface $order Order */ public function createOrderCouponsByCartCoupons(CartInterface $cart, OrderInterface $order) { $cartCouponAmount = $cart->getCouponAmount(); if ($cartCouponAmount instanceof MoneyInterface) { $order->setCouponAmount($cartCouponAmount); } /** * @var CouponInterface $coupons */ $coupons = $this->cartCouponManager->getCoupons($cart); if (empty($coupons)) { return; } /** * Before applying Coupons to Order, we should remove old references * if exist. */ $this->orderCouponTruncator->truncateOrderCoupons($order); /** * An event is dispatched for each convertible coupon. */ foreach ($coupons as $coupon) { $this->orderCouponEventDispatcher->dispatchOrderCouponOnApplyEvent($order, $coupon); } }
/** * Calculates coupons price given actual Cart. * * @param CartInterface $cart Cart */ public function refreshCouponAmount(CartInterface $cart) { $couponAmount = Money::create(0, $this->currencyWrapper->get()); $coupons = $this->cartCouponManager->getCoupons($cart); foreach ($coupons as $coupon) { $currentCouponAmount = $this->getCouponAbsolutePrice($cart, $coupon); $coupon->setAbsolutePrice($currentCouponAmount); $couponAmount = $couponAmount->add($currentCouponAmount); } $cart->setCouponAmount($couponAmount); }
/** * A new Order has been created. * * This method adds Coupon logic in this transformation * * @param OrderOnCreatedEvent $orderOnCreatedEvent OrderOnCreated Event * * @return null */ public function convertCouponToOrder(OrderOnCreatedEvent $orderOnCreatedEvent) { $order = $orderOnCreatedEvent->getOrder(); $cart = $orderOnCreatedEvent->getCart(); $cartCouponAmount = $cart->getCouponAmount(); if ($cartCouponAmount instanceof MoneyInterface) { $order->setCouponAmount($cartCouponAmount); } /** * @var CouponInterface[]|Collection $coupons */ $coupons = $this->cartCouponManager->getCoupons($cart); if ($coupons->isEmpty()) { return null; } /** * Before applying Coupons to Order, we should remove old references * if exist. Otherwise, */ $this->truncateOrderCoupons($order); /** * An event is dispatched for each convertible coupon */ foreach ($coupons as $coupon) { $this->orderCouponEventDispatcher->dispatchOrderCouponOnApplyEvent($order, $coupon); } }
/** * Get current cart automatic coupons. * * @param CartInterface $cart Cart * * @returns CouponInterface[] Array of non applied coupons */ private function getNonAppliedAutomaticCoupons(CartInterface $cart) { $automaticCoupons = $this->couponRepository->findBy(['enforcement' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC]); $loadedAutomaticCoupons = $this->cartCouponManager->getCoupons($cart); return array_udiff($automaticCoupons, $loadedAutomaticCoupons, function (CouponInterface $a, CouponInterface $b) { return $a->getId() != $b->getId(); }); }
/** * Method subscribed to CartLoad event. * * Calculates coupons price given actual Cart, and overrides Cart price * given new information. * * @param CartOnLoadEvent $event */ public function refreshCouponAmount(CartOnLoadEvent $event) { $cart = $event->getCart(); $couponAmount = Money::create(0, $this->currencyWrapper->get()); $coupons = $this->cartCouponManager->getCoupons($cart); foreach ($coupons as $coupon) { $currentCouponAmount = $this->getPriceCoupon($cart, $coupon); $coupon->setAbsolutePrice($currentCouponAmount); $couponAmount = $couponAmount->add($currentCouponAmount); } $cart->setCouponAmount($couponAmount); $cart->setAmount($cart->getAmount()->subtract($couponAmount)); }
/** * Method subscribed to CartLoad event. * * Calculates coupons price given actual Cart, and overrides Cart price * given new information. * * @param CartOnLoadEvent $cartOnLoadEvent Event */ public function onCartLoadCoupons(CartOnLoadEvent $cartOnLoadEvent) { $cart = $cartOnLoadEvent->getCart(); $couponAmount = Money::create(0, $this->currencyWrapper->loadCurrency()); $coupons = $this->cartCouponManager->getCoupons($cart); /** * @var CouponInterface $coupon */ foreach ($coupons as $coupon) { $currentCouponAmount = $this->getPriceCoupon($cart, $coupon); $coupon->setAbsolutePrice($currentCouponAmount); $couponAmount = $couponAmount->add($currentCouponAmount); } $cart->setCouponAmount($couponAmount); $cart->setAmount($cart->getAmount()->subtract($couponAmount)); }