2

I need to display price in my custom template

   $productCollections = $productCollections->addAttributeToSelect('*')
                      ->addAttributeToFilter('status', 1)
                      ->addAttributeToFilter('is_sale', 1)
                     // ->addAttributeToFilter('show_in_home', 1)
                      ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
                      ->load()
foreach($productCollections as $productCollection){
  echo $productCollection->getName();
}

I am using above code to implement product listing in home page.

I need to display price of product with html style

$this->getPriceHtml($product);

3 Answers 3

8

For phtml use this :

<?php /* @escapeNotVerified */ echo $block->getProductPrice($_product) ?>

For block just inject Magento\Catalog\Block\Product\ListProduct into your block

4
  • You can get both simple and configurable product collection then how do u get price for config product which is shown in list page
    – Jaisa
    Commented Feb 22, 2018 at 3:19
  • Is this GetPtoductPrice () can differentiate itself which product is coming from collection to get price?
    – Jaisa
    Commented Feb 22, 2018 at 3:21
  • @RonakChauhan It's not working after I inject this block in my block. Can you please tell me how to solve this? Commented Oct 12, 2018 at 13:12
  • Here my question : magento.stackexchange.com/q/246245/51810 Commented Oct 12, 2018 at 13:40
3

Block do not longer have method getPriceHtml. You have to pass by another block to render a price, here the doc.

As you can see you have to make the following call into your block or template:

$this->getLayout()->getBlock('product.price.render.default')->render(
    '<price type>',
    $product,
    $arguments
);

The price code will be one of those: http://devdocs.magento.com/guides/v2.0/architecture/behavior/pricing.html#m2devgde-pricelib-def.

base_price is pretty good in general. :)

Hope this help.

1
  • 1
    final_price is used in the catalog_category_list by default.
    – Scorsi
    Commented Dec 22, 2016 at 13:45
1

Try This Code

Block File

protected $resultLayout;
protected $_productloader;

public function __construct(
    ..............................
    \Magento\Catalog\Model\ProductFactory $_productloader,
    \Magento\Framework\View\Result\Layout $resultLayout,
    ..............................
) {
    ..............................
    $this->resultLayout = $resultLayout;
    $this->_productloader = $_productloader;
    ..............................
}

..............................

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;
}

public function getProductData($id)
{
    return $this->_productloader->create()->load($id);
}

phtml File

$productid = 1088;

$product = $block->getProductData($productid);

echo $block->getProductPriceHtml($product);

I Hope This Helps You.

Your Answer

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

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