Product price in Magento 2

create category custom layout in Magento 2

Get product price in Magento 2,

Get Product Price Including Tax,

get the special price of products in Magento 2.

1.Magento 2 get Product Price Including Tax

<?php
 
namespace Vendor\Module\Helper;
 
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Helper\Data as TaxHelper;
 
class Data extends AbstractHelper
{
    protected $productModel;
    protected $taxHelper;
 
    public function __construct(
        Context $context,
        Product $productModel,
        TaxHelper $taxHelper
    )
    {
        $this->productModel = $productModel;
        $this->taxHelper = $taxHelper;
        parent::__construct($context);
    }
 
    public function getProductPrice($productId)
    {
        $product = $this->productModel->load($productId);
        $price = $this->taxHelper->getTaxPrice($product, $product->getFinalPrice(), true);
        return $price;
    }
}

2.Magento 2 get Product Price Tax

/**
     * @param \Magento\Catalog\Model\Product $product
     * @return string
     */
    public function getProductPrice($product)
    {
        $priceRender = $this->getLayout()->getBlock('product.price.render.default')
            ->setData('is_product_list', true);

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

        return $price;
    }  

3.Get Special Price of the Product

 /**
     * Get Special Price Product
     */
    public function getSpecialPriceById($productId)
    {
        $product = $this->productModel->load($productId);
        return $product->getSpecialPrice();
    }

4.Get product final price

public function getProductFinalPrice($productId)
    {
        $product = $this->productModel->load($productId);
        $price =  $product->getFinalPrice();
        return $price;
    }

5.Get product price

public function getProductPrice($productId)
{
   $product = $this->productModel->load($productId);
   $price =  $product->getPrice();
   return $price;
}

Related Post : magento2 get category image

Like us on Facebook and Linkedin for more updates.

Back To Top