1

In Magento 2 there is more than one method to get product by id. Anyone show me how to get product y in any of the method step by step include files name nd path used.

2
  1. Inject Magento\Catalog\Api\ProductRepositoryInterface in your constructor and assign it to a variable in the class.
 $this->productRepository = $productRepository;
  1. Get the product by it's SKU -
 $product = $this->productRepository->get($productSku);
  1. Get the product by it's ID -
$product = $this->productRepository->getById($id);

This is the recommended way to access a data from database in Magento.

| improve this answer | |
0

Object method

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

Factory Method

<?php
namespace Test\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;  


  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader

    ) {


        $this->_productloader = $_productloader;
        parent::__construct($context);
    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }

}

phtml file code

$product=$this->getLoadProduct(20);
echo $product->getName();


<?php
    namespace Test\Module\Block;
    use Magento\Catalog\Api\ProductRepositoryInterface;
    class Product extends \Magento\Framework\View\Element\Template
     {
          protected $productRepository; 
          protected $_storeManager; 

          public function __construct(
            \Magento\Framework\App\Action\Context $context,
            ProductRepositoryInterface $productRepository
          ) {
              parent::__construct($context);

              $this->productRepository = $productRepository;
          }
          public function getProduct()
          {

              $productId=1;
              return $product = $this->productRepository->getById($productId);
          }
      }
| improve this answer | |
0

Here I used the easiest way to get the ProductName by ID.

use this code in your Controller <vendor name>/<module>/Controller/Index/Index.php


<?php
namespace <vendor name>\<module>\Controller\Index;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
 protected $_pageFactory,$productId=731,$productModel;
 public function __construct(Context $context,Product $productModel)
{
 parent::__construct($context);
 $this->productModel = $productModel;
 $this->getProductModel();
}
 public function execute()
{
 return $this->_pageFactory->create();
}
 public function getProductModel()
{
 $product=$this->productModel->load($this->productId);
 echo $product->getName();
 }
}

| improve this answer | |
0

Add block and Controller file like this

protected $_productloader;

public function __construct(
    ........................................
    \Magento\Catalog\Model\ProductFactory $_productloader,
    ........................................
) {
    ........................................
    $this->_productloader = $_productloader;
    ........................................
}
public function getLoadProduct($id)
{
    return $this->_productloader->create()->load($id);
}

In controler and block you can call using

$product_id = 45;
$data = $this->getLoadProduct($product_id);
print_r($data->getData());

In phtml file call

$product_id = 45;
$data = $block->getLoadProduct($product_id);
print_r($data->getData());

block and phtml link like this (add your block file path )

<block class="VendoreName\ModuleName\Block\YourBlockName" name="prd.data" template="VendoreName_ModuleName::yourfilname.phtml" >
| improve this answer | |

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.