/** * Cart view * * @param FormView $formView Form view * @param CartInterface $cart Cart * * @return Response Response * * @Route( * path = "", * name = "store_cart_view", * methods = {"GET"} * ) * * @AnnotationEntity( * class = { * "factory" = "elcodi.wrapper.cart", * "method" = "get", * "static" = false, * }, * name = "cart" * ) * @AnnotationForm( * class = "store_cart_form_type_cart", * name = "formView", * entity = "cart", * ) */ public function viewAction(FormView $formView, CartInterface $cart) { $relatedProducts = []; if ($cart->getCartLines()->count()) { $relatedProducts = $this->get('elcodi_store.provider.product_collection')->getRelatedProducts($cart->getCartLines()->first()->getProduct(), 3); } $cartCoupons = $this->get('elcodi.manager.cart_coupon')->getCartCoupons($cart); return $this->renderTemplate('Pages:cart-view.html.twig', ['cart' => $cart, 'cartCoupons' => $cartCoupons, 'form' => $formView, 'related_products' => $relatedProducts]); }
/** * Cart view * * @param FormType $formType Form type * @param CartInterface $cart Cart * * @return array * * @AnnotationEntity( * class = { * "factory" = "elcodi.cart_wrapper", * "method" = "loadCart", * "static" = false, * }, * name = "cart" * ) * @AnnotationForm( * class = "store_cart_form_type_cart", * name = "formView", * entity = "cart", * ) */ public function viewAction(FormType $formType, CartInterface $cart) { $relatedProducts = []; if ($cart->getCartLines()->count()) { $relatedProducts = $this->get('store.product.service.product_collection_provider')->getRelatedProducts($cart->getCartLines()->first()->getProduct(), 3); } /** * Let's sort the cartLines if needed */ $cart->setCartLines($this->get('elcodi.cart_lines_sorter')->sortCartLines($cart->getCartLines())); $cartCoupons = $this->get('elcodi.cart_coupon_manager')->getCartCoupons($cart); $formView = $this->get('form.factory')->create($formType, $cart)->createView(); return $this->render('StoreCartBundle:Cart:view.html.twig', ['cart' => $cart, 'cartcoupon' => $cartCoupons, 'form' => $formView, 'related_products' => $relatedProducts]); }
/** * Flushes all loaded cart and related entities. * * We only persist it if have lines loaded inside, so empty carts will never * be persisted * * @param CartInterface $cart Cart */ public function saveCart(CartInterface $cart) { if (!$cart->getCartLines()->isEmpty()) { $this->cartObjectManager->persist($cart); } $this->cartObjectManager->flush(); }
/** * Calculate coupon absolute value. * * @param CartInterface $cart Cart * @param CouponInterface $coupon Coupon * * @return MoneyInterface|false Absolute value for this coupon in this cart */ public function getCouponAbsoluteValue(CartInterface $cart, CouponInterface $coupon) { $currency = $this->currencyWrapper->get(); $couponPrice = Money::create(0, $currency); $value = $coupon->getValue(); preg_match($this->regexp(), $value, $match); $m = (int) $match[1]; $n = (int) $match[2]; $expressionValue = isset($match[3]) ? $match[3] : ''; $modifiers = isset($match[4]) ? $match[4] : ''; $freePerGroup = $m - $n; $freePerGroup = max($freePerGroup, 0); foreach ($cart->getCartLines() as $cartLine) { $moneys = []; $purchasable = $cartLine->getPurchasable(); $expressionEvaluator = $this->getExpressionLanguageInstance(); $expressionResult = (empty($expressionValue) || $expressionEvaluator->evaluate($expressionValue, ['purchasable' => $purchasable])) && $this->evaluatePurchasableType($purchasable, $modifiers); if (true === $expressionResult) { $partialElements = $cartLine->getQuantity(); for ($i = 0; $i < $partialElements; ++$i) { $partialPurchasable = $cartLine->getPurchasable(); $moneys[] = $partialPurchasable->getReducedPrice()->getAmount() > 0 ? $partialPurchasable->getReducedPrice() : $partialPurchasable->getPrice(); } $groups = $partialElements / $m; if ($groups > 0) { $nbMoneys = $groups * $freePerGroup; $moneysToDiscount = array_slice($moneys, 0, $nbMoneys); foreach ($moneysToDiscount as $moneyToDiscount) { $couponPrice = $couponPrice->add($this->currencyConverter->convertMoney($moneyToDiscount, $currency)); } } } } return $couponPrice; }
/** * Validate cart integrity. * * @param CartInterface $cart Cart */ public function validateCartIntegrity(CartInterface $cart) { /** * Check every CartLine. * * @var CartLineInterface $cartLine */ foreach ($cart->getCartLines() as $cartLine) { $this->validateCartLine($cartLine); } }
/** * Calculates and load how many purchasables has this cart. * * @param CartInterface $cart Cart */ public function loadCartPurchasablesQuantities(CartInterface $cart) { $quantity = 0; /** * Calculate max shipping delay. */ foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ $quantity += $cartLine->getQuantity(); } $cart->setQuantity($quantity); }
/** * 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 CartInterface $cart Cart */ public function tryAutomaticCoupons(CartInterface $cart) { if ($cart->getCartLines()->isEmpty()) { return; } $automaticCoupons = $this->getNonAppliedAutomaticCoupons($cart); foreach ($automaticCoupons as $coupon) { try { $this->cartCouponManager->addCoupon($cart, $coupon); } catch (AbstractCouponException $e) { // Silently tries next coupon on controlled exception } } }
/** * Load cart purchasables prices. * * @param CartInterface $cart Cart */ public function loadCartPurchasablesAmount(CartInterface $cart) { $currency = $this->currencyWrapper->get(); $purchasableAmount = Money::create(0, $currency); /** * Calculate Amount and PurchasableAmount. */ foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ $cartLine = $this->loadCartLinePrices($cartLine); /** * @var MoneyInterface $purchasableAmount * @var MoneyInterface $totalAmount */ $convertedPurchasableAmount = $this->currencyConverter->convertMoney($cartLine->getPurchasableAmount(), $currency); $purchasableAmount = $purchasableAmount->add($convertedPurchasableAmount->multiply($cartLine->getQuantity())); } $cart->setPurchasableAmount($purchasableAmount); }
/** * Purchasable related view * * @param CartInterface $cart Cart * * @return array * * @Route( * path = "/related", * name = "store_cart_related", * methods = {"GET"} * ) * * @AnnotationEntity( * class = { * "factory" = "elcodi.wrapper.cart", * "method" = "get", * "static" = false, * }, * name = "cart" * ) */ public function relatedAction(CartInterface $cart) { $purchasables = []; $cartLines = $cart->getCartLines(); /** * @var CartLineInterface $cartLine */ foreach ($cartLines as $cartLine) { $purchasables[] = $cartLine->getPurchasable(); } $relatedPurchasables = $this->get('elcodi.related_purchasables_provider')->getRelatedPurchasablesFromArray($purchasables, 3); return $this->renderTemplate('Modules:_purchasable-related.html.twig', ['purchasables' => $relatedPurchasables]); }
/** * Remove a Purchasable from Cart. * * This method removes a Purchasable from the Cart. * * If the Purchasable is already in the Cart, it just decreases * item quantity by $quantity * * @param CartInterface $cart Cart * @param PurchasableInterface $purchasable Product or Variant to add * @param int $quantity Number of units to set or increase * * @return $this Self object */ public function removePurchasable(CartInterface $cart, PurchasableInterface $purchasable, $quantity) { /** * If quantity is not a number or is 0 or less, product is not removed * from cart. */ if (!is_int($quantity) || $quantity <= 0) { return $this; } foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ if (get_class($cartLine->getPurchasable()) === get_class($purchasable) && $cartLine->getPurchasable()->getId() == $purchasable->getId()) { /** * Product already in the Cart, decrease quantity. */ return $this->decreaseCartLineQuantity($cartLine, $quantity); } } return $this; }
/** * Common asserts for a test that empties lines. * * @param CartLineInterface $line The CartLineInterface needed */ private function assertRemovedLine(CartLineInterface $line) { $this->assertEmpty($this->cart->getCartLines()); $this->assertNotNull($this->cart->getId()); $this->assertEquals(UnitOfWork::STATE_NEW, $this->get('elcodi.object_manager.cart_line')->getUnitOfWork()->getEntityState($line)); }
/** * This method calculates all quantities given a Cart * * @param CartInterface $cart Cart * * @return CartInterface Cart */ private function calculateCartQuantities(CartInterface $cart) { $quantity = 0; /** * Calculate max shipping delay */ foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ $quantity += $cartLine->getQuantity(); } $cart->setQuantity($quantity); return $cart; }
/** * Add a Purchasable to Cart as a new CartLine * * This method creates a new CartLine and set item quantity * correspondingly. * * If the Purchasable is already in the Cart, it just increments * item quantity by $quantity * * @param CartInterface $cart Cart * @param PurchasableInterface $purchasable Product or Variant to add * @param integer $quantity Number of units to set or increase * * @return $this self Object */ public function addProduct(CartInterface $cart, PurchasableInterface $purchasable, $quantity) { /** * If quantity is not a number or is 0 or less, product is not added * into cart */ if (!is_int($quantity) || $quantity <= 0) { return $this; } foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ if ($cartLine->getPurchasable()->getId() == $purchasable->getId()) { /** * Product already in the Cart, increase quantity */ return $this->increaseCartLineQuantity($cartLine, $quantity); } } $cartLine = $this->cartLineFactory->create(); $cartLine->setPurchasable($purchasable)->setQuantity($quantity); $this->addLine($cart, $cartLine); return $this; }