/** * 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); } }
/** * * display a amount in expected format * * available parameters : * number => int or float number * decimals => how many decimals format expected * dec_point => separator for the decimal point * thousands_sep => thousands separator * symbol => Currency symbol * * ex : {format_money number="1246.12" decimals="1" dec_point="," thousands_sep=" " symbol="€"} will output "1 246,1 €" * * @param $params * @param null $template * @throws \TheliaSmarty\Template\Exception\SmartyPluginException * @return string the expected number formatted */ public function formatMoney($params, $template = null) { $number = $this->getParam($params, "number", false); if ($number === false || $number === '') { return ""; } if ($this->getParam($params, "symbol", null) === null) { return MoneyFormat::getInstance($this->request)->formatByCurrency($number, $this->getParam($params, "decimals", null), $this->getParam($params, "dec_point", null), $this->getParam($params, "thousands_sep", null), $this->getParam($params, "currency_id", null)); } return MoneyFormat::getInstance($this->request)->format($number, $this->getParam($params, "decimals", null), $this->getParam($params, "dec_point", null), $this->getParam($params, "thousands_sep", null), $this->getParam($params, "symbol", null)); }