31

I got the product collection in my custom template using the following snippet:

<?php 
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection
  $productCollection */
  $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
 /** Apply filters here */

 $productCollection->load();

 foreach ($productCollection as $p)
 {
  echo "<pre>";
  print_R($p->getData());
 }
Here i got array with product id like this

 Array
(
[entity_id] => 1
[attribute_set_id] => 4
[type_id] => virtual
[sku] => Double Corona Natural (box of 25)
[has_options] => 0
[required_options] => 0
[created_at] => 2015-12-23 09:20:38
[updated_at] => 2015-12-23 09:20:38
[is_salable] => 1
)

From this entity_id, how can I load products like we did in Magento 1.x?

$_product = Mage::getModel('catalog/product')->load($productID);  
3

1 Answer 1

106

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();

In Magento 2.1

<?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);
          }
      }

Reference

14
  • 21
    Never use object manager directly to load model. Use factories instead. See: magento.stackexchange.com/a/113038/2294
    – Arvind07
    Commented Jun 12, 2016 at 12:15
  • 2
    NOT USE THIS ANSWER, but see new magento directive devdocs.magento.com/guides/v2.1/extension-dev-guide/…
    – LucScu
    Commented Jul 6, 2017 at 7:18
  • @QaisarSatti, $product_title = $product->getName(); returning 500 server error Commented Dec 18, 2018 at 8:45
  • @jafarpinjar kindly share your code. Commented Dec 18, 2018 at 9:00
  • @QaisarSatti, I have configurable product id so can i get only configurable products detail not associated products using product repository ? Commented Apr 10, 2019 at 6:53

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