0

How to get product ID Price, if I have some many product,

I want like this get the product ID Price

<?php echo $block->getProductPrice($_product(ID)) ?>

for example: i have two products ID is 1, 2. and i want get this two products price to showing in my custom page. just want to get the product price.

How can I do it?

Thank you!

  • update,please see the post again, i want get Every product price, like product (id) , want to showing product price for my custom page.@PrathapGunasekaran – niceonelee Mar 5 at 11:53
  • Try my answer and it will work – Prathap Gunasekaran Mar 5 at 12:16
2

Try this,

Add the below code in you block

<?php                                                                 
namespace Vendor\ModuleName\Block;                                          
use Magento\Framework\View\Element\Template;                              
class GetPricce extends Template                                              
{
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepo
) {     
    $this->productRepo = $productRepo;
}

public function getProductPrice($productId = '')
{
    $product = $this->productRepo->getById($productId);
    return $product->getPrice();
}}

The above should be in your block and your phtml you can call like

<?php echo $block->getProductPrice($_product(ID)) ?>

Hope this helps :)

  • ModuleName is my theme name or Magento_Theme? if my custom file in app/design/frontend/Abc/Magento_Theme/templates , please help, thank you – niceonelee Mar 5 at 12:25
  • Block should go under custom module, and you should add the phtml to respective block through xml. Please let me know it you need any more information – Prathap Gunasekaran Mar 5 at 12:33
  • My file full path in app/design/frontend/Abc/Magento_Theme/templates/demo.phtml, so i change [ModuleName to Magento_Theme? , can you help? thank you please – niceonelee Mar 5 at 12:36
  • The module in app/code/123/Abc – niceonelee Mar 5 at 12:39
  • See you should create a module first then you have to add a block and map to your phtml and hoping that's where you get your product ids. Thanks – Prathap Gunasekaran Mar 5 at 12:41
0

Using object manager (not recommended):

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->load(product_id_here);
$productPriceById = $productCollection->getPrice();
echo $productPriceById;
?>
  • Hi, i want get Every product price, like $_product(id:1), $_product(id:2) – niceonelee Mar 5 at 11:55
  • want to showing product price for my custom page. – niceonelee Mar 5 at 11:56
  • Ahhh, sorry, you question was a bit difficult to understand :/ – Greg Mar 5 at 11:58
  • for exsample: i have two products ID is 1, 2. and i want get this two products price to showing in my custom page. just want to get the this product price. thank you – niceonelee Mar 5 at 12:00
  • 1
    I think @PrathapGunasekaran gave you the answer you are looking for. – Greg Mar 5 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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