Get Special Price data by Product SKU in Magento 2. Special price is the price, which offers discount/sale to the original price.
You need to use interface,
Magento\Catalog\Api\SpecialPriceInterface to retrieve special price collection with price_from, price_to, store_id and sku.
Just Passing Product SKU as an array ([$sku]) in the get($sku) method of SpecialPriceInterface.
<?php
namespace Jesadiya\SpecialPrice\Model;
use Exception;
use Magento\Catalog\Api\SpecialPriceInterface;
class GetSpecialPrice
{
/**
* @var SpecialPriceInterface
*/
private $specialPrice;
/**
* constructor.
*
* @param SpecialPriceInterface $specialPrice
*/
public function __construct(
SpecialPriceInterface $specialPrice
) {
$this->specialPrice = $specialPrice;
}
/**
* @param string
* @return array
*/
public function getSpecialPriceData($sku)
{
try {
$product = $this->specialPrice->get([$sku]);
} catch (Exception $exception) {
throw new Exception($exception->getMessage());
}
return $product;
}
}
Pass SKU value to get speicific products, special price data,
$specialPrice = $this->getSpecialPriceData($sku);
Output:
Product has special price, it will display array of result,
Array
(
[0] => Array
(
[row_id] => 1
[value] => 29.99
[store_id] => 0
[sku] => 24-MB01
[price_from] => 1970-01-01 00:00:01
[price_to] => 2020-01-19 03:14:07
)
)
If Product hasn’t special price result will be empty array.
