/** * Get the price and, if in sale, promo price for a product, adjusted with the selected attribute values. * Prices are with taxes and formatted for display. * * @return JsonResponse */ public function getPricesAction() { $baseForm = $this->createForm('thelia.cart.add'); // make the quantity field a dummy field so that we can ignore it // (we want to be able to update prices on out-of-stock products), // while still validating the rest of the form $baseForm->getForm()->remove('quantity'); $baseForm->getForm()->add('quantity', 'number', ['mapped' => false]); try { $form = $this->validateForm($baseForm, 'POST'); $product = ProductQuery::create()->findPk($form->get('product')->getData()); $productGetPricesEvent = (new ProductGetPricesEvent($product->getId()))->setCurrencyId($this->getSession()->getCurrency()->getId())->setLegacyProductAttributes($this->getLegacyProductAttributesInForm($form)); $this->getDispatcher()->dispatch(LegacyProductAttributesEvents::PRODUCT_GET_PRICES, $productGetPricesEvent); if (null !== $productGetPricesEvent->getPrices()) { $prices = $productGetPricesEvent->getPrices(); } else { $prices = new ProductPriceTools(0, 0); } $moneyFormat = MoneyFormat::getInstance($this->getRequest()); /** @var TaxEngine $taxEngine */ $taxEngine = $this->getContainer()->get('thelia.taxEngine'); $taxCountry = $taxEngine->getDeliveryCountry(); $taxCalculator = (new Calculator())->load($product, $taxCountry); $response = ['price' => $moneyFormat->format($taxCalculator->getTaxedPrice($prices->getPrice()), null, null, null, $this->getSession()->getCurrency()->getSymbol())]; if ($product->getDefaultSaleElements()->getPromo()) { $response['promo_price'] = $moneyFormat->format($taxCalculator->getTaxedPrice($prices->getPromoPrice()), null, null, null, $this->getSession()->getCurrency()->getSymbol()); } return new JsonResponse($response); } catch (FormValidationException $e) { return JsonResponse::createError($e->getMessage(), 400); } }
/** * try to attach a new item to an existing cart * * @param EventDispatcherInterface $dispatcher * @param \Thelia\Model\Cart $cart * @param int $productId * @param ProductSaleElements $productSaleElements * @param float $quantity * @param ProductPriceTools $productPrices * * @return CartItem */ protected function doAddItem(EventDispatcherInterface $dispatcher, CartModel $cart, $productId, ProductSaleElements $productSaleElements, $quantity, ProductPriceTools $productPrices) { $cartItem = new CartItem(); $cartItem->setDisptacher($dispatcher); $cartItem->setCart($cart)->setProductId($productId)->setProductSaleElementsId($productSaleElements->getId())->setQuantity($quantity)->setPrice($productPrices->getPrice())->setPromoPrice($productPrices->getPromoPrice())->setPromo($productSaleElements->getPromo())->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60 * 60 * 24 * 30))->save(); return $cartItem; }