/**
  * 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);
 }
Esempio n. 3
0
 /**
  * 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);
         }
     }
 }
 /**
  * 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);
     }
 }
 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);
 }
 /**
  * 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 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
         }
     }
 }
 /**
  * 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));
 }
Esempio n. 9
0
 /**
  * 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));
 }
 /**
  * Creates a new CartCoupon instance.
  *
  * @param CartCouponOnApplyEvent $event Event
  */
 public function createAndSaveCartCoupon(CartCouponOnApplyEvent $event)
 {
     $cartCoupon = $this->cartCouponManager->createAndSaveCartCoupon($event->getCart(), $event->getCoupon());
     $event->setCartCoupon($cartCoupon);
 }
 /**
  * Removes Coupon from Cart, and flushes it.
  *
  * @param CartCouponOnRemoveEvent $event Event
  */
 public function removeCartCoupon(CartCouponOnRemoveEvent $event)
 {
     $this->cartCouponManager->removeCartCoupon($event->getCartCoupon());
 }