Question

How can I get Original price and Final price of given below types product?

  1. Simple product
  2. Configurable product
  3. Bundle product
  4. Group product

For simple product I can get price easily using below code.

$finalPrice = $product->getFinalPrice();
$originalPrice = $product->getPrice();

But I am not able to get Original price and Final price for Configurable product, Bundle product, Group product

Is there any easy way to get both prices of all other types of product.


EDIT :

I get price Original price and Final price of configurable product using below code. and take reference from get-price-range-configurable-product-magento-2

$basePrice = $product->getPriceInfo()->getPrice('regular_price');

$regularPrice = $basePrice->getMinRegularAmount()->getValue();
$specialPrice = $product->getFinalPrice();

Any help would be appreciated! Thanks.

Was it helpful?

Solution

You can get Regular price and Final price of all types of product using below way.

  1. Simple Product
$regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue();
$specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue();
  1. Configurable product
if ($product->getTypeId() == 'configurable') {
      $basePrice = $product->getPriceInfo()->getPrice('regular_price');

      $regularPrice = $basePrice->getMinRegularAmount()->getValue();
      $specialPrice = $product->getFinalPrice();
}
  1. Bundle product
if ($product->getTypeId() == 'bundle') {
      $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getMinimalPrice()->getValue();
      $specialPrice = $product->getPriceInfo()->getPrice('final_price')->getMinimalPrice()->getValue();            
}
  1. Group product
if ($product->getTypeId() == 'grouped') {
      $usedProds = $product->getTypeInstance(true)->getAssociatedProducts($product);            
      foreach ($usedProds as $child) {
          if ($child->getId() != $product->getId()) {
                $regularPrice += $child->getPrice();
                $specialPrice += $child->getFinalPrice();
          }
      }
}

Note : In above example $product is current product.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top