1

How can I get product final_price attribute (affected by a catalog price rule) in a block? I've tried in this way without success: $this->_productFactory->create()->load($idProd);

I've found price attribute only.

How can I do?

2
  • I guess it depends what Block you're in. You could also override the current Block to add logic to get the product's price. Dec 6, 2016 at 15:06
  • I'm in my custom block (class Discount extends \Magento\Framework\View\Element\Template) and it is called in product/list.phtml template. I would like to calculate the product discount so I need price and final_price attributes.
    – WaPoNe
    Dec 6, 2016 at 15:12

3 Answers 3

5

I wanted to get the final_price for the product in /product/view/hero.phtml. So that I could display the stickers based on the discount. Below is what worked for me after spending some time in Magento core:

$finalPriceModel = $_product->getPriceInfo()->getPrice('final_price');

It will return the instance of Magento\Catalog\Pricing\Price\FinalPrice. Which has a method getValue() method defined to return the amount in float. So to just get the amount:

$finalPriceAmt = $_product->getPriceInfo()->getPrice('final_price')->getValue();

I haven't tried this code snippet on list.phtml. But it should work as long as $_product is an instance of Magento\Catalog\Model\Product\Interceptor.

1

Debugging (by Xdebug) I noted that $this->_productFactory->create()->load($idProd) object contains price property but not final_price one, so trying to print $this->_productFactory->create()->load($idProd)->getData('final_price'); it doesn't return a value; it works if you try with $this->_productFactory->create()->load($idProd)->getFinalPrice();

0
getProduct()->getSpecialPrice() != null): ?> Old Price : formatPrice($item->getProduct()->getPrice());?>

formatPrice($block->getUnitDisplayPriceInclTax()) ?>

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.