7

Is there anyway I can display the price of a certain product by id or sku?

something like this piece of code:

$_product->getPrice();

But I need to be able to put a sku or id somewhere in that code to display a price of a product on another page.

*I need to be able to do it on a cms page

3
  • Please check my answer and let me know if it's not working. Commented May 17, 2018 at 13:19
  • You can upvote and accept answer if it's helpful for you. So, other user can use this code. Commented May 17, 2018 at 13:20
  • It's not working on an cms page @RohanHapani
    – DevAnd
    Commented May 17, 2018 at 13:27

2 Answers 2

5

Use \Magento\Catalog\Model\ProductFactory class to get price of product

=> Factory Method :

protected $_product;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    $this->productFactory = $productFactory;
}

public function getPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->productFactory->create();
    $productPriceById = $product->load($id)->getPrice();
    return $productPriceById;
}

public function getPriceBySku($sku)
{   
    //$sku = 'testing'; //Product sku
    $product = $this->productFactory->create();
    $productPriceBySku = $product->loadByAttribute('sku', $sku)->getPrice();
    return $productPriceBySku;
}

=> Object Manager Method :

BY Id :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->load(your-product-id);
$productPriceById = $productCollection->getPrice();

By Sku :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', your-sku);
$productPriceBySku = $productCollection->getPrice();
7
  • Anyway to do it on an cms page?
    – DevAnd
    Commented May 17, 2018 at 13:21
  • You can do it by object manager. But, it's not proper way. Commented May 17, 2018 at 13:22
  • How can I do that?
    – DevAnd
    Commented May 17, 2018 at 13:29
  • Check my updated answer Commented May 18, 2018 at 13:33
  • Hi but when I put that into an cms page the front end just shows me the code.
    – DevAnd
    Commented May 18, 2018 at 13:54
1

Hi Please try below code

<?php

    $productId = 8 //product id

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $productRepository  = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
    $product = $productRepository->getById($productId);


    $abstractProductBlock = $block->getLayout()->createBlock('\Magento\Catalog\Block\Product\AbstractProduct');     
    echo $price =$abstractProductBlock->getProductPrice($product);


?>
2
  • Anyway of doing it on an cms page?
    – DevAnd
    Commented May 17, 2018 at 13:21
  • yes you can, add template in cms page and add code in template Commented Jun 4, 2018 at 12:33

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.