Magento2: get price with and without tax on list.phtml

Created on 19 May 2016  路  14Comments  路  Source: magento/magento2

Hello,

how we can get the price for simple and configurable product on list.phtml? If we try:

$block->getProductPrice($_product)

We get the correct price but how we get the price with and without tax from a simple and configurable product? Which 2 line codes we need to use?

like:

$productPriceWithTax = samplecode ;
$productPriceWithOutTax = samplecode ;

Hope someone can help... :(

Most helpful comment

Hi there, Seppelchen, I believe that you are referring to Magento/Catalog/view/frontend/templates/product/list.phtml
You could use something like
$_product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount()
This gets you the price with no tax into store currency
You might want to convert that into display currency
$this->priceCurrency->convert() where is \Magento\Framework\Pricing\PriceCurrencyInterface and inject that into your block through constructor
Better implement a method for that and not do a lot of things into phtml

Don't use getProductPrice because it really depends on your configurations if catalog has tax included or excluded, and of your display configurations for catalog page

If you don't want to use this for calculation but just for display you could use renderers,
$block->getProductPrice($_product) gets the renderer for final price
you could implement your own $block->getProductBasePrice() in a class that extends MagentoCatalogBlockProductListProduct

/**
     * @param \Magento\Catalog\Model\Product $product
     * @return string
     */
    public function getProductBasePrice(\Magento\Catalog\Model\Product $product)
    {
        $priceRender = $this->getPriceRender();

        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE,
                $product,
                [
                    'include_container' => false,
                    'display_minimal_price' => true,
                    'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
                ]
            );
        }

        return $price;
    }

All 14 comments

Hi there, Seppelchen, I believe that you are referring to Magento/Catalog/view/frontend/templates/product/list.phtml
You could use something like
$_product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount()
This gets you the price with no tax into store currency
You might want to convert that into display currency
$this->priceCurrency->convert() where is \Magento\Framework\Pricing\PriceCurrencyInterface and inject that into your block through constructor
Better implement a method for that and not do a lot of things into phtml

Don't use getProductPrice because it really depends on your configurations if catalog has tax included or excluded, and of your display configurations for catalog page

If you don't want to use this for calculation but just for display you could use renderers,
$block->getProductPrice($_product) gets the renderer for final price
you could implement your own $block->getProductBasePrice() in a class that extends MagentoCatalogBlockProductListProduct

/**
     * @param \Magento\Catalog\Model\Product $product
     * @return string
     */
    public function getProductBasePrice(\Magento\Catalog\Model\Product $product)
    {
        $priceRender = $this->getPriceRender();

        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE,
                $product,
                [
                    'include_container' => false,
                    'display_minimal_price' => true,
                    'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
                ]
            );
        }

        return $price;
    }

Hello, thank you for that great replay.
You talking:
$_product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount()

and what we can use for product with tax (which will also work for simple and configurable product)?

Hope you could also provide a similar code. :)

this code should work for all product types
$_product->getPriceInfo()->getPrice('final_price')->getAmount()->getAmount()
is the price including tax (that adds Weee FPT price as well if you use that)
You can look at the format of $_product->getPriceInfo()->getPrice('final_price')->getAmount()
it has all your needed information there like discounts, adjustments, tax values if more.. etc..

Hello, sorry for question, but...
If we are using

$_product->getPriceInfo()->getPrice('final_price')->getAmount()

How we can convert into for example:
11.11 or 10.00
--> Round and use 2 decimals.
If we try:

round($_product->getPriceInfo()->getPrice('final_price')->getAmount(),2)

wont work and also not with number_format
Hope you can help again...:S

sure, you can use Magento/Framework/Pricing/PriceCurrencyInterface

     /**
     * Format price value
     *
     * @param float $amount
     * @param bool $includeContainer
     * @param int $precision
     * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
     * @param \Magento\Framework\Model\AbstractModel|string|null $currency
     * @return float
     */
    public function format(
        $amount,
        $includeContainer = true,
        $precision = self::DEFAULT_PRECISION,
        $scope = null,
        $currency = null
    );

Hi sorry I cant understand.
I had insert into list.phtml:

$priceWithTaxNotRound = $_product->getPriceInfo()->getPrice('final_price')->getAmount();
$priceWithTaxRound =  $priceWithTaxNotRound;
echo $priceWithTaxRound;

What I need write into line with "$priceWithTaxRound" that it will be round and display everytime 2 decimals like:
10.00 or 11.32

you will have to inject that interface into your extended Magento/Catalog/Block/Product/ListProduct.php through constructor,
http://magento.stackexchange.com/questions/74632/di-extending-a-block-on-magento-2/74633#74633

in constructor you can inject your Magento/Framework/Pricing/PriceCurrencyInterface
then use it in the block

you might look how it's done in Magento/AdvancedCheckout/Block/Adminhtml/Manage/Items.php
public function formatPrice, and do it in your own block

Hello,
your help are very brilliant but I cant understand how...:'(
How I can insert round price into list.phtml ?
I never do that before. 馃拑

No, beacuse the block doesn't have the propper tools to do it.
you're doing too much on list.phtml, that's not the purpose for a view, that's why we have renderers
if you want to add more business logic, you must implement your own block and do that logic there as I described
Don't be scared to add a little code, Magento is all about best practices and doing things the right way,
It has the right libraries but you need to modify your code to have access to them
It hapends that the block you're in doesn't have formatting because it uses renderers that do the formatting directly

Extend your block, and add those formatters so you can use them as ListProduct is not intended for that
You can look at this treads for more info or at http://devdocs.magento.com/

http://magento.stackexchange.com/questions/95697/magento-2-override-magento-catalog-block-product-view-block

http://magento.stackexchange.com/questions/74632/di-extending-a-block-on-magento-2

Hello,
OK. For example, but thats not working because I cant using:

$_product->getPriceInfo()->getPrice('final_price')->getAmount()

Example code:

$priceWithoutTaxNotRound = $_product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount();
$priceWithTaxNotRound = $_product->getPriceInfo()->getPrice('final_price')->getAmount();
$productTax = ($priceWithoutTaxRound-$priceWithTaxNotRound);
echo $productTax;

How can I put my code working and into block? Could you subscribe for beginners and very easy that o can do that step by step please?

May you could develop a *.php example and let me know where I need put that and which code I should use to display on list.phtml

So I could learn how it will working on example. It would be very nice and great.

Use
$product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue() as $_product->getPriceInfo()->getPrice('final_price')->getAmount() is an object
Do you have a debugger? you could do a breakpoint and see what's the structure

You could try to ask a question on http://magento.stackexchange.com as this is not a bug but more of a question how to, and I'll try to respond that there

Hello,
it made me really really sad... :-(

If I am using

echo $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();

I get

otice: Undefined variable: product in /var/www/vhosts/domain.tld/httpdocs/magento2/app/design/frontend/Emthemes/everything/default/Magento_Catalog/templates/product/list.phtml on line 352

0 /var/www/vhosts/domain.tld/httpdocs/magento2/app/design/frontend/Emthemes/everything/default/Magento_Catalog/templates/product/list.phtml(352): MagentoFrameworkAppErrorHandler->handler(8, 'Undefined varia...', '/var/www/vhosts...', 352, Array)

1 /var/www/vhosts/domain.tld/httpdocs/magento2/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/vhosts...')

2 /var/www/vhosts/domain.tld/httpdocs/magento2/vendor/magento/framework/View/Element/Template.php(255): MagentoFrameworkViewTemplateEnginePhp->render(Object(MagentoCatalogBlockProductListProductInterceptor), '/var/www/vhosts...', Array)

3 /var/www/vhosts/domain.tld/httpdocs/magento2/var/generation/Magento/Catalog/Block/Product/ListProduct/Interceptor.php(557): MagentoFrameworkViewElementTemplate->fetchView('/var/www/vhosts...')

... and so on....

But:

$_product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue()

is working.

Now it is working like a charm. :) Thank you for great support. If we now could also solve that issue:

4581 (Magento 1.0.5. - Discount on shipping cost tax conflict)

I would pretty happy. Maybe you also can help?

Closing; this is a question better asked on Stack Exchange or our Forum (though it's answered).

Was this page helpful?
0 / 5 - 0 ratings