Skip to main content
Updated answer
Source Link
Rizwan Khan
  • 2k
  • 2
  • 18
  • 41

Use the below code in your class or helper.

protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product_id = $block->getProduct()->getId();
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

 public function getPriceById($id)
{
    $product = $this->_productRepository->create();
    return $product->load($id)->getPrice();
}

public function getPriceBySku($sku)
{    
 
    $product = $this->_productRepository->create();
    return $product->loadByAttribute('sku', $sku)->getPrice();
}

call this function using block or helper as you want.

call this function using block or helper as you want.

$id = '1';
$orginalPrice = $_product->getPrice();
$sku = 'some-ski';

//special price
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);

//normal price
$block->getPriceById($id);
$block->getPriceBySku($sku);

Please try the below code if you want to use it with object manager but it is highly not recommended.

$product_id = $block->getProduct()->getId();
$_product = $objectManager->get('Magento\Catalog\Model\ProductRepository')->create()->load($product_id);
 
$orginalPrice = $_product->getPrice();
$specialPrice = $_product->getSpecialPrice();
protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

public function getPriceById($id)
{
    $product = $this->_productRepository->create();
    return $product->load($id)->getPrice();
}

public function getPriceBySku($sku)
{   
    $product = $this->_productRepository->create();
    return $product->loadByAttribute('sku', $sku)->getPrice();
}

call this function using block or helper as you want.

$id = '1';
$sku = 'some-ski';

//special price
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);

//normal price
$block->getPriceById($id);
$block->getPriceBySku($sku);

Use the below code in your class or helper.

protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product_id = $block->getProduct()->getId();
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

 public function getPriceById($id)
{
    $product = $this->_productRepository->create();
    return $product->load($id)->getPrice();
}

public function getPriceBySku($sku)
{    
 
    $product = $this->_productRepository->create();
    return $product->loadByAttribute('sku', $sku)->getPrice();
}

call this function using block or helper as you want.

$id = '1';
$orginalPrice = $_product->getPrice();
$sku = 'some-ski';

//special price
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);

//normal price
$block->getPriceById($id);
$block->getPriceBySku($sku);

Please try the below code if you want to use it with object manager but it is highly not recommended.

$product_id = $block->getProduct()->getId();
$_product = $objectManager->get('Magento\Catalog\Model\ProductRepository')->create()->load($product_id);
 
$orginalPrice = $_product->getPrice();
$specialPrice = $_product->getSpecialPrice();
Improved Answer
Source Link
Rizwan Khan
  • 2k
  • 2
  • 18
  • 41
protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
} 

public function getPriceById($id)
{
    $product = $this->_productRepository->create();
    return $product->load($id)->getPrice();
}

public function getPriceBySku($sku)
{   
    $product = $this->_productRepository->create();
    return $product->loadByAttribute('sku', $sku)->getPrice();
}

call this function using block or helper as you want.

$id = '1';
$sku = 'some-ski'; 

//special price
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);

//normal price
$block->getPriceById($id);
$block->getPriceBySku($sku);
protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

call this function using block or helper as you want.

$id = '1';
$sku = 'some-ski';
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);
protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
} 

public function getPriceById($id)
{
    $product = $this->_productRepository->create();
    return $product->load($id)->getPrice();
}

public function getPriceBySku($sku)
{   
    $product = $this->_productRepository->create();
    return $product->loadByAttribute('sku', $sku)->getPrice();
}

call this function using block or helper as you want.

$id = '1';
$sku = 'some-ski'; 

//special price
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);

//normal price
$block->getPriceById($id);
$block->getPriceBySku($sku);
Source Link
Rizwan Khan
  • 2k
  • 2
  • 18
  • 41

protected $_productRepository;

public function __construct(   
    \Magento\Catalog\Model\ProductRepository $productRepository,
) {
    $this->_productRepository = $productRepository;
}

public function getSpecialPriceById($id)
{
    $product = $this->_productRepository->getById($id);
    return $product->getSpecialPrice();
}

public function getSpecialPriceBySku($sku)
{   
    $product = $this->_productRepository->get($sku);
    return $product->getSpecialPrice();
}

call this function using block or helper as you want.

$id = '1';
$sku = 'some-ski';
$block->getSpecialPriceById($id);
$block->getSpecialPriceBySku($sku);