Magento 2 - Get Original Price and Special Price of a Bundle Product
Pregunta
I want to get the original price of a Bundle product (Minimal/Maximal one). This needs to replicate what Magento is grabbing for the WAS price below so I can add a percentage off to product pages.
I can get the Special Price like below:
$bundleObj=$product->getPriceInfo()->getPrice('final_price');
echo $bundleObj->getMinimalPrice();// For min price
echo $bundleObj->getMaximalPrice(); // for max price
I've tried using the below but this just returns 0:
$_product->getPrice();
This also only has both with and without tax and the standalone tax:
print_r($_product->getPriceInfo()->getPrice('final_price')->getMinimalPrice());
Have also looked at:
$_product->getPriceModel()->getTotalPrices($_product,'min',1)
This seems to just be pulling similar data cannot get the original price.
Solución
After studying a lot of files containing the old-price
tag similar to that used in the markup for the WAS price, I found the below:
$finalPriceModel = $block->getPriceType('regular_price');
echo priceModel->getAmount();
vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml
I figured it made sense to use regular_price
instead of final_price
and so used similar code in my block to get the original price:
$bundleRegularPrice = $_product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
So after playing a little bit I can confirm how to get the following prices:
Regular Minimal Price:
$_product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
Regular Maximal Price:
$_product->getPriceInfo()->getPrice('regular_price')->getMaximalPrice()->getValue();
Special Minimal Price:
$_product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();
Special Maximal Price:
$_product->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getValue();
These are all including the tax to get the value excluding tax change getValue()
to getBaseAmount();
.
e.g:
$_product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getBaseAmount();
Otros consejos
if tax enable
list($minimalPrice, $maximalPrice) = $item->getPriceModel()->getTotalPrices($item, null, true, false);
if tax is not there
list($minimalPrice, $maximalPrice) = $item->getPriceModel()->getTotalPrices($item, null, false, false);
You will get the Minimum price as $minimalPrice and the Maximum price as $maximalPrice.Please let me know if you still face the issue.