php 如何在 Prestashop 的产品列表中一次显示含税和不含税的产品价格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23237952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:36:07  来源:igfitidea点击:

How to display product price with and without tax at a time in product list for Prestashop?

phpprestashopprestashop-1.6

提问by Leo T Abraham

In the product list I need to display the product price with and without tax at a time.

在产品列表中,我需要一次显示含税和不含税的产品价格。

I am using the version 1.6 of Prestashop.

我正在使用 Prestashop 的 1.6 版。

Right now the price including tax is displayed in the product list. I want to display the price excluding tax as well.

现在产品列表中显示的是含税价格。我也想显示不含税的价格。

How can I do that? I have searched for solution and was not able to find a working solution for me.

我怎样才能做到这一点?我一直在寻找解决方案,但找不到适合我的解决方案。

回答by yenshirak

Find the following block in product-list.tpl:

在 中找到以下块product-list.tpl

{foreach from=$products item=product name=products}

Add this to display price without tax:

添加此以显示不含税的价格:

{convertPrice price=$product.price_tax_exc}

Make sure that during development Template compilationis set to Force compilationand Cacheis set to Noin PrestaShop back-office -> Advanced Parameters->?Performance.

确保在开发期间在 PrestaShop 后台Template compilation设置为Force compilationCache设置为No-> Advanced Parameters->? Performance.

回答by PawelW

In my case it works for default tax excl.:

在我的情况下,它适用于默认税不包括:

{convertPrice price=$product->getPrice(false, $smarty.const.NULL)} ({l s='tax excl.'})

回答by focalizer

I have a similar problem in order list before checkout. The error message displays the total amount and product amount without tax. So i modified the file in controllers > front > OrderController.php (PS 1.6) At line 63

我在结帐前的订单列表中有类似的问题。错误消息显示不含税的总金额和产品金额。所以我修改了控制器 > 前 > OrderController.php (PS 1.6) 在第 63 行中的文件

// Check minimal amount
    $currency = Currency::getCurrency((int)$this->context->cart->id_currency);

    $orderTotal = $this->context->cart->getOrderTotal();
    $minimal_purchase = Tools::convertPrice((float)Configuration::get('PS_PURCHASE_MINIMUM'), $currency);   

    if ($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimal_purchase && $this->step > 0) {
        $_GET['step'] = $this->step = 0;
        $this->errors[] = sprintf(  
            Tools::displayError('A minimum purchase total of %1s (tax excl.) is required to validate your order, current purchase total is %2s (tax excl.).'),
            Tools::displayPrice($minimal_purchase_2, $currency), Tools::displayPrice($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS), $currency)    
        );                      
    }

with the following code

使用以下代码

// Check minimal amount
    $currency = Currency::getCurrency((int)$this->context->cart->id_currency);

    $orderTotal = $this->context->cart->getOrderTotal();
    $minimal_purchase = Tools::convertPrice((float)Configuration::get('PS_PURCHASE_MINIMUM'), $currency);

    # modified (total amount included tax - only for screen error)

    $minimal_purchase_2 = round(Tools::convertPrice((float)Configuration::get('PS_PURCHASE_MINIMUM'), $currency)*1.22,1);       
    $productTotal = round($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS)*1.22,1);

    if ($this->context->cart->getOrderTotal(false, Cart::ONLY_PRODUCTS) < $minimal_purchase && $this->step > 0) {
        $_GET['step'] = $this->step = 0;
        $this->errors[] = sprintf(                 
            Tools::displayError('A minimum purchase total of %1s (tax incl.) is required to validate your order, current purchase total is %2s (tax incl.).'),  
            Tools::displayPrice($minimal_purchase_2, $currency), Tools::displayPrice($productTotal, $currency)
        );                      
    }

I have to solve to get the actual tax value (at the moment i inserted 1.22 for italy tax value).

我必须解决才能获得实际的税值(目前我为意大利的税值插入了 1.22)。

At the end you have to translate in localization the new sentence. Hope someone can complete or better solve this question.

最后,您必须以本地化方式翻译新句子。希望有人能完成或更好地解决这个问题。

回答by manuman94

I know there is already one accepted answer but I needed more information about how to get a product price.

我知道已经有一个已被接受的答案,但我需要更多有关如何获取产品价格的信息。

The Prestashop built-in product class has the getPrice method.

Prestashop 内置产品类具有 getPrice 方法。

/**
* Get product price
* Same as static function getPriceStatic, no need to specify product id
*
* @param bool $tax With taxes or not (optional)
* @param int $id_product_attribute Product attribute id (optional)
* @param int $decimals Number of decimals (optional)
* @param int $divisor Util when paying many time without fees (optional)
* @return float Product price in euros
*/
public function getPrice($tax = true, $id_product_attribute = null, $decimals = 6,
    $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1)
{
    return Product::getPriceStatic((int)$this->id, $tax, $id_product_attribute, $decimals, $divisor, $only_reduc, $usereduc, $quantity);
}

As you can see you can specify if you want it with taxes, the number of decimals given as result, and the number divisor.

如您所见,您可以指定是否需要含税、作为结果给出的小数位数以及除数。

So, if you want to get the product price by ID with and without taxes you can achieve it like this

因此,如果您想通过带税和不带税的 ID 获取产品价格,您可以像这样实现

$product = new Product($id_product, $id_language) // Fill with your info
$price_with_taxes = $product->getPrice(true);
$price_wout_taxes = $product->getPrice(false);

As other comments say, if you are inside a template, you can get the product id depending on the view you are modifying.

正如其他评论所说,如果您在模板中,则可以根据正在修改的视图获取产品 ID。

In product.tpl (the single product view) there is a $product variable. In product-list.tpl you have the $products variable, an array containing all products showing in the list.

在 product.tpl(单一产品视图)中有一个 $product 变量。在 product-list.tpl 中有 $products 变量,这是一个包含列表中显示的所有产品的数组。

Hope this helps.

希望这可以帮助。

回答by Harsh

Simple solution

简单的解决方案

Go to Customers -> Groups and click Edit on the group you want to modify:

转到“客户”->“组”,然后在要修改的组上单击“编辑”:

Find Price display method option and select Price included or excluded as you want then Save changes:

查找价格显示方法选项并根据需要选择包含或排除的价格,然后保存更改:

check with pressing ctrl + f5. Done

按 ctrl + f5 进行检查。完毕