Magento 2 - get special price of product by product id or sku
Use \Magento\Catalog\Model\ProductRepository
class to get the special price of the product
protected $_productRepository;
public function __construct(
...
\Magento\Catalog\Model\ProductRepository $productRepository,
...
) {
$this->_productRepository = $productRepository;
}
public function getSpecialPriceById($id)
{
//$id = '21'; //Product ID
$product = $this->_productRepository->getById($id);
return $product->getSpecialPrice();
}
public function getSpecialPriceBySku($sku)
{
//$sku = 'test-21'; //Product Sku
$product = $this->_productRepository->get($sku);
return $product->getSpecialPrice();
}
Now you can get special price by
$id = '21';
$sku = 'test-21';
$this->getSpecialPriceById($id);
$this->getSpecialPriceBySku($sku);
use Magento/Catalog/Model/ProductFactory
to load product, and get price information from the model
Code is like:
$specialPrice = $this->productFactory()->create()->load($id)->getPriceInfo()->getPrice('special_price');
some reference about how to get product model in your custom module
use object manager: http://alanstorm.com/magento_2_object_manager_instance_objects/
use dependency injection: http://www.coolryan.com/magento/2016/01/27/dependency-injection-in-magento-2/
Normally dependency injection is recommended.