1

I have listed some products in my custom home page template.

I have tried to get price html in home page but its not working.

In magento 1.9 below code is used to load price template

 $_product = Mage::getModel('catalog/product')->load($product->getId());
      $productBlock = $this->getLayout()->createBlock('catalog/product_price');
      echo $productBlock->getPriceHtml($_product);

How to load this price template in magento 2

I tried below code

$blockObj= $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\ListProduct');
                echo $blockObj->getProductPrice($_product);

But it returns 1 rupee as a price

2 Answers 2

3

You have to get product price html using magento 2 below way,

You have to override \Magento\Catalog\Block\Product\ListProduct block and get your product object and call below function in your template file.

public function getPrice(\Magento\Catalog\Model\Product $product)
{
    $priceRender = $this->getLayout()->getBlock('product.price.render.default');
    if (!$priceRender) {
        $priceRender = $this->getLayout()->createBlock(
            \Magento\Framework\Pricing\Render::class,
            'product.price.render.default',
            ['data' => ['price_render_handle' => 'catalog_product_prices']]
        );
    }

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

    return $price;
}
2
0

Try This Code

protected $resultLayout;

public function __construct(
    \Magento\Framework\View\Result\Layout $resultLayout
) {
    $this->resultLayout = $resultLayout;
}

public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
{
    /** @var \Magento\Framework\Pricing\Render $priceRender */
    $priceRender = $this->resultLayout->getLayout()->getBlock(\Magento\Framework\Pricing\Render::class);
    if (!$priceRender) {
        $priceRender = $this->resultLayout->getLayout()->createBlock(
            \Magento\Framework\Pricing\Render::class,
            \Magento\Framework\Pricing\Render::class,
            ['data' => ['price_render_handle' => 'catalog_product_prices']]
        );
    }
    $price = '';
    if ($priceRender) {
        $price = $priceRender->render(
            \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
            $product,
            [
                'display_minimal_price' => true,
                'use_link_for_as_low_as' => true,
                'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
            ]
        );
    }

    return $price;
}

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.