5

I'm trying to show a discount in my products, so I created a Helper to get the price and the FinalPrice. With those values I suppose to get the discount but I realized the Price and FinalPrice of my products are the same value. Ex:

$originalPrice = $product->getPrice();  //200
$finalPrice = $product->getFinalPrice(); //200
$specialPrice = $product->getSpecialPrice(); //0 or X value when the product has special price config in Products
$RefinalPrice = $product->getPriceModel()->getFinalPrice(1,$product);// Final Price including Catalog Price Rules

am I missing something here? Is there a better way to get those values?

4
  • Welcome to Magento Stack Exchange :) U need to use finalprice
    – Jackson
    Commented Oct 28, 2016 at 4:05
  • But It's not correct, I have a 10% discount in my Catalog Price Rules and that discount is not apply in the value that I get from finalPrice. So maybe I'm missing something. Commented Oct 28, 2016 at 4:06
  • Refer Magento 1 magento.stackexchange.com/questions/13153/… Function will be same
    – Jackson
    Commented Oct 28, 2016 at 4:07
  • Thank you so much for take your time to answer me, I'll take a look! Commented Oct 28, 2016 at 4:09

3 Answers 3

10

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.

Additionally, below code will give you the regular price:

$_product->getPriceInfo()->getPrice('regular_price')

Haven't tried it for special and tier prices.

0
8

We had to get the final price from a CRON process so our solution was first to set current store

/** @var \Magento\Store\Model\StoreManager */
$storeManager->setCurrentStore(1);

/** @var \Magento\Catalog\Model\Product $product */
$finalPrice = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
$oldPrice = $product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
0

I got the solution, the way to do it is using getPrice and getFinalPrice from the Product entity as @ankit-shah recommends in his comment, basically if you get the product entity from a Collection still you will need to get the Product entity by the model itself. Something like this:

$product = $this->productLoader->create()->load($_product->getId()); 

with this you will get the correct values. Thanks Ankit!

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.