0

I am trying to get the configurable product's special price or final price inside a plugin class. I already tried the below methods but it will not fetch any special price or final price of the configurable product's data. Also, I tried to view configurable product's simple product data using its type instance used products but I couldn't find any special or final price data in its simple products as well.

Tried methods:

1. Using \Magento\Catalog\Model\Product $product

$product = $this->product->load($item['product_id']);
$specialPrice = $product->getSpecialPrice();
$finalPrice = $product->getFinalPrice();

2. Using \Magento\Catalog\Model\ProductRepository $productRepository

$product = $this->product->getProductById($item['product_id']);
$specialPrice = $product->getSpecialPrice();
$finalPrice = $product->getFinalPrice();

FYI - Also, I checked in $product->getData() as well but only available the price value which doesn't have the special price or final price

Expected result: Get the special or final price value and passed it to an email template content variables inside the plugin class function. This special price value correctly showing on the configurable product details page.

Actual result: null or 0 shows as the get data

Could you please let me know your thoughts? Is there any other way to get the special or final price of the configurable product inside a plugin class?

Thanks!

2 Answers 2

0

Try $product->getPriceInfo()->getPrice('special_prics') using product repo or factory

1
0

Finally, I found a way to sort this out by using the below class

Magento\Catalog\Model\Product\Price\PricePersistence

I will share the steps below so I hope it will save someone else time who faced this same issue.

1. Pass attribute code value as special_price in di.xml in your module

magento/codepool/app/code/Vendor/Module/etc/di.xml

<type name="Magento\Catalog\Model\Product\Price\PricePersistence">
   <arguments>
     <argument name="attributeCode" xsi:type="string">special_price</argument>
   </arguments>
</type>

2. Inject the Magento\Catalog\Model\Product\Price\PricePersistence to your plugin class

/**
* @var \Magento\Catalog\Model\Product\Price\PricePersistence
*/
protected $pricePersistence;

public function __construct(
   \Magento\Catalog\Model\Product\Price\PricePersistence $pricePersistence
)
{
   $this->pricePersistence = $pricePersistence;
}

3. Define a custom method to get the special price of a simple product of the configurable product if configurable $product->getSpecialPrice() value is not available

/**
 * Get configurable product special price if getSpecialPrice value
 * not available
 * @param null $configurableProduct
 * @return int|mixed
 * @access public
 */
 public function getSimpleProductSpecialPrice($configurableProduct = null) {
   $specialPrice = 0;
   if (!is_null($configurableProduct)) {
     $simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
     if (is_array($simpleProducts) && !is_null($simpleProducts)) {
       $skuData = [];
       foreach ($simpleProducts as $simpleProduct) {
         $skuData[] = $simpleProduct->getSku();
       }
       $specialPriceData = $this->pricePersistence->get($skuData);
       if (is_array($specialPriceData) && !is_null($specialPriceData)) {
         foreach ($specialPriceData as $row) {
           if ($row['value'] > 0) {
             $specialPrice = $row['value'];
             break;
           }
         }
       }
     }
   }
   return $specialPrice;
 }

Your Answer

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

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