I am developing a custom module with a custom block which displays a list of products.

My Block class:

use \Magento\Framework\View\Element\Template;

class Products extends Template
    protected $_productCollectionFactory;

     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
     * @param array $data
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = []
        $this->_productCollectionFactory = $productCollectionFactory;
        parent::__construct($context, $data);

    public function getHomeProducts()

        $collection = $this->_productCollectionFactory->create();
        return $collection;

.phtml file:

$productCollection = $block->getHomeProducts();
<?php if($productCollection->count() > 0) : ?>
    <div class="lrnt_hp_products">
            <?php foreach ($productCollection as $product) : ?>
                    <a href="<?php echo $product->getProductUrl(); ?>">
                        <img src="<?php echo $block->getUrl('pub/media/catalog') . 'product' . $product->getImage(); ?>" alt="<?php echo $product->getName(); ?>" />
                    <h2><a href="<?php echo $product->getProductUrl(); ?>"><?php echo $product->getName(); ?></a></h2>
                    // here I would like to print the price
            <?php endforeach; ?>
<?php endif; ?>

I am trying to show product price in the phtml file but could not find proper way to show the price.

I now I can print it with:


and searching which automatically prints the final_price, the special-price if these are present etc.

How can I achieve this?

  • $product->getFinalPrice() prints only the discounted price, I would like to print both the regular and the discounted price
    – giani.sim
    Jul 27, 2017 at 8:36

1 Answer 1


To fetch the Product price in custom module same as listing page using price render block. Add the below function in your custom block class.

 * @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(
                'include_container' => true,
                'display_minimal_price' => true,
                'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                'list_category_page' => true

    return $price;

and Call the function in template like below and pass Product Object as argument:

<?php echo $block->getProductPrice($product); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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