/** * When a cart goes below 0 (due to discounts), set the amount to 0. * * @param CartOnLoadEvent $event Event */ public function limitCartAmount(CartOnLoadEvent $event) { $cart = $event->getCart(); $amount = $cart->getAmount(); if ($amount->getAmount() <= 0) { $cart->setAmount(Money::create(0, $amount->getCurrency())); } }
/** * If the cart's shipping amount is not defined, then put an empty Money * value * * @param CartOnLoadEvent $event Event * * @return $this Self object */ public function checkEmptyShippingAmount(CartOnLoadEvent $event) { $cart = $event->getCart(); if (!$cart->getShippingAmount() instanceof Money) { $cart->setShippingAmount($this->emptyMoneyWrapper->get()); } return $this; }
/** * Performs all processes to be performed after the order creation * * Flushes all loaded order and related entities. * * @param CartOnLoadEvent $event Event * * @return $this Self object */ public function loadShippingPrice(CartOnLoadEvent $event) { $cart = $event->getCart(); $shippingMethodId = $cart->getShippingMethod(); if (empty($shippingMethodId)) { return $this; } $shippingMethod = $this->shippingWrapper->getOneById($cart, $shippingMethodId); if ($shippingMethod instanceof ShippingMethod) { $cartAmount = $cart->getAmount(); $convertedShippingAmount = $this->currencyConverter->convertMoney($shippingMethod->getPrice(), $cartAmount->getCurrency()); $cart->setAmount($cartAmount->add($convertedShippingAmount)); } return $this; }
/** * 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 } } }
/** * Load cart shipping amount. * * @param CartOnLoadEvent $event Event */ public function loadCartShippingAmount(CartOnLoadEvent $event) { $this->cartShippingAmountLoader->loadCartShippingAmount($event->getCart()); }
/** * Method subscribed to CartLoad event * * 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 CartOnLoadEvent $event Event */ public function refreshCartCoupons(CartOnLoadEvent $event) { $this->cartCouponValidator->validateCartCoupons($event->getCart()); }
/** * Load cart purchasables quantity */ public function loadCartPurchasablesQuantities(CartOnLoadEvent $event) { $this->cartPurchasablesQuantityLoader->loadCartPurchasablesQuantities($event->getCart()); }
/** * If the cart's shipping amount is not defined, then put an empty Money * value. * * @param CartOnLoadEvent $event Event */ public function validateEmptyShippingAmount(CartOnLoadEvent $event) { $this->cartShippingAmountValidator->validateEmptyShippingAmount($event->getCart()); }
/** * Flushes all loaded cart and related entities. * * We only persist it if have lines loaded inside, so empty carts will never * be persisted * * @param CartOnLoadEvent $event Event */ public function saveCart(CartOnLoadEvent $event) { $cart = $event->getCart(); if (!$cart->getCartLines()->isEmpty()) { $this->cartObjectManager->persist($cart); } $this->cartObjectManager->flush(); }
/** * Stores cart id in HTTP session when this is saved * * @param CartOnLoadEvent $event Event */ public function saveCartInSession(CartOnLoadEvent $event) { $this->cartSessionManager->set($event->getCart()); }
/** * 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)); }
/** * Stores cart id in HTTP session when this is saved * * @param CartOnLoadEvent $event Event */ public function onCartLoad(CartOnLoadEvent $event) { $this->cartSessionManager->set($event->getCart()); }
/** * Method subscribed to CartLoad event. * * Calculates coupons price given actual Cart * * @param CartOnLoadEvent $event */ public function refreshCouponAmount(CartOnLoadEvent $event) { $this->cartCouponPricesLoader->refreshCouponAmount($event->getCart()); }
/** * Loads cheapest shipping method if exists * * @param CartOnLoadEvent $event Event * * @return $this Self object */ public function loadCheapestShippingMethod(CartOnLoadEvent $event) { $cart = $event->getCart(); $cartShippingMethodId = $cart->getShippingMethod(); /** * We don't have the need to find the cheapest one if the real one is * already defined */ if (null !== $cartShippingMethodId) { return $this; } /** * If the cart is not associated to any customer, just skip it */ if (!$cart->getCustomer() instanceof CustomerInterface) { return $this; } $address = $cart->getDeliveryAddress() instanceof AddressInterface ? $cart->getDeliveryAddress() : $cart->getCustomer()->getAddresses()->first(); /** * If the user does'nt have any address defined, we cannot approximate * anything */ if (!$address instanceof AddressInterface) { return $this; } $cart->setDeliveryAddress($address); $validShippingMethods = $this->shippingWrapper->get($cart); if (!empty($validShippingMethods)) { $cheapestShippingMethod = $this->shippingResolver->getCheapestShippingMethod($validShippingMethods); $cart->setCheapestShippingMethod($cheapestShippingMethod->getId()); } return $this; }
/** * Load cart total amount. */ public function loadCartTotalAmount(CartOnLoadEvent $event) { $this->cartPricesLoader->loadCartTotalAmount($event->getCart()); }
/** * 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 */ public function tryAutomaticCoupons(CartOnLoadEvent $event) { $this->automaticCouponApplicator->tryAutomaticCoupons($event->getCart()); }
/** * Load cart quantities. * * This event listener should be subscribed after the cart flush action * * @param CartOnLoadEvent $event Event */ public function onCartLoadQuantities(CartOnLoadEvent $event) { $this->loadCartQuantities($event->getCart()); }