/** * Remove shipping method from cart if this is not valid anymore * * @param CartOnLoadEvent $event Event * * @return $this Self object */ public function removeInvalidShippingMethod(CartOnLoadEvent $event) { $cart = $event->getCart(); $cartShippingMethodId = $cart->getShippingMethod(); if (null === $cartShippingMethodId) { return $this; } $shippingMethod = $this->shippingWrapper->getOneById($cart, $cartShippingMethodId); if (!$shippingMethod instanceof ShippingMethod) { $cart->setShippingMethod(null); $this->cartEventDispatcher->dispatchCartLoadEvents($cart); $event->stopPropagation(); } return $this; }
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); }
/** * Sets quantity to cartLine * * If quantity is higher than item stock, throw exception * * This method dispatches all Cart Check and Load events * * @param CartLineInterface $cartLine Cart line * @param integer $quantity CartLine quantity to set * * @return $this self Object */ public function setCartLineQuantity(CartLineInterface $cartLine, $quantity) { $cart = $cartLine->getCart(); if (!$cart instanceof CartInterface) { return $this; } /** * If $quantity is an integer and is less or equal than 0, means that * full line must be removed. * * Otherwise, $quantity can have two values: * * null or false - Quantity is not affected * * integer higher than 0, quantity is edited and all changes are * recalculated. */ if (is_int($quantity) && $quantity <= 0) { $this->silentRemoveLine($cart, $cartLine); } elseif (is_int($quantity)) { $cartLine->setQuantity($quantity); $this->cartLineEventDispatcher->dispatchCartLineOnEditEvent($cart, $cartLine); } else { /** * Nothing to do here. Quantity value is not an integer, so will not * be treated as such */ return $this; } $this->cartEventDispatcher->dispatchCartLoadEvents($cart); return $this; }
/** * Adds cartLine to Cart. * * This method dispatches all Cart Check and Load events * It should NOT be used to add a Purchasable to a Cart, * by manually passing a newly crafted CartLine, since * no product duplication check is performed: in that * case CartManager::addProduct should be used * * @param CartInterface $cart Cart * @param CartLineInterface $cartLine Cart line * * @return $this Self object */ private function addLine(CartInterface $cart, CartLineInterface $cartLine) { $cartLine->setCart($cart); $cart->addCartLine($cartLine); $this->cartLineEventDispatcher->dispatchCartLineOnAddEvent($cart, $cartLine); $this->cartEventDispatcher->dispatchCartLoadEvents($cart); return $this; }
/** * Load cart * * This method, first of all tries to retrieve cart from session * * If this does not exists nor the id is not valid, a new cart is created * using Cart factory * * This behavior can be overriden just overwritting the wrapper * * @return CartInterface Instance of Cart loaded */ public function loadCart() { $customer = $this->customerWrapper->loadCustomer(); $cartFromCustomer = $this->getCustomerCart($customer); $cartFromSession = $this->getCartFromSession(); $this->cart = $this->resolveCarts($customer, $cartFromCustomer, $cartFromSession); $this->cartEventDispatcher->dispatchCartLoadEvents($this->cart); return $this->cart; }
/** * Get loaded object. If object is not loaded yet, then load it and save it * locally. Otherwise, just return the pre-loaded object. * * @return mixed Loaded object */ public function get() { if ($this->cart instanceof CartInterface) { return $this->cart; } $customer = $this->customerWrapper->get(); $cartFromCustomer = $this->getCustomerCart($customer); $cartFromSession = $this->cartSessionWrapper->get(); $this->cart = $this->resolveCarts($customer, $cartFromCustomer, $cartFromSession); $this->cartEventDispatcher->dispatchCartLoadEvents($this->cart); return $this->cart; }
/** * Check CartLine integrity. * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ private function validateCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); $realStockAvailable = $this->purchasableStockValidator->isStockAvailable($purchasable, $cartLine->getQuantity(), $this->useStock); if (false === $realStockAvailable || $realStockAvailable === 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched. */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); return $cartLine; } if (is_int($realStockAvailable)) { $cartLine->setQuantity($realStockAvailable); } return $cartLine; }
/** * Check CartLine integrity * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ protected function checkCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $cartLine->getQuantity() <= 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); } /** * We cannot exceed available stock for a given purchasable * when setting CartLine::$quantity */ if ($cartLine->getQuantity() > $purchasable->getStock()) { $cartLine->setQuantity($purchasable->getStock()); } return $cartLine; }
/** * Check CartLine integrity * * When a purchasable is not enabled or its quantity is <=0, * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT * event is fired. * * A further check on stock availability is performed so that when * $quantity is greater that the available units, $quantity for this * CartLine is set to Purchasable::$stock number * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface CartLine */ private function checkCartLine(CartLineInterface $cartLine) { $cart = $cartLine->getCart(); $purchasable = $cartLine->getPurchasable(); if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $this->useStock && $cartLine->getQuantity() <= 0) { $this->cartManager->silentRemoveLine($cart, $cartLine); /** * An inconsistent cart event is dispatched */ $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine); } /** * We cannot exceed available stock for a given purchasable * when setting CartLine::$quantity * * This checking has sense when the Product has not infinite stock */ if ($this->useStock && $cartLine->getProduct()->getStock() !== ElcodiProductStock::INFINITE_STOCK && $cartLine->getQuantity() > $purchasable->getStock()) { $cartLine->setQuantity($purchasable->getStock()); } return $cartLine; }
/** * Update cart after a success addition. * * @param AbstractCartCouponEvent $event Event */ public function updateCart(AbstractCartCouponEvent $event) { $this->cartEventDispatcher->dispatchCartLoadEvents($event->getCart()); }