1

i have a phtml file where i want to get special and regular price of product im using product model but getting nothing, im using this code:

$product_id =$block->getProduct()->getId();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
 
$orgprice = $_product->getPrice();
$specialprice = $_product->getSpecialPrice();
1
  • Hello @nihal try this code $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productData = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); echo "<pre/>"; print_r($productData); Commented Jul 9, 2020 at 10:44

2 Answers 2

1

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();
5
  • i cannot use constructs as i am working in phtml file Commented Jul 9, 2020 at 10:40
  • i can use object manager Commented Jul 9, 2020 at 10:40
  • 2
    it is not recommended practice to write business logic in phtml, also don't use objectmanager as it jacks up the readability. Use blocks to get data/ business logic as suggested in answer. Commented Jul 9, 2020 at 10:42
  • yes i know this but for now i have to do this using object manager just to test several logics Commented Jul 9, 2020 at 11:00
  • using the above code with object manager gives this error : Call to undefined method Magento\Catalog\Model\ProductRepository\Interceptor::create() Commented Jul 9, 2020 at 15:41
1

You can use the Magento\Catalog\Model\ProductRepository to fetch the prices

1
  • not working also Commented Jul 9, 2020 at 10:34

Your Answer

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

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