Using Dependency Injection (DI)
Here is the example code to get the product information by product Id and SKU in Magento 2 using dependency injection.
In this, we might need to inject the object of the \Magento\Catalog\Model\ProductRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.
Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php
<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductRepository $productRepository,
array $data = []
) {
$this->_productRepository = $productRepository;
parent::__construct($context, $data);
}
public function getProductById($id) {
return $this->_productRepository->getById($id);
}
public function getProductBySku($sku) {
return $this->_productRepository->get($sku);
}
}
Now, we can use the functions in our view (.phtml) file as follows.
// get product by id
$product = $block->getProductById(15);
// get product by sku
$product = $block->getProductBySku('MT12');
echo $product->getEntityId() . '<br>';
echo $product->getName() . '<br>';
echo $product->getSKU() . '<br>';
echo $product->getPrice() . '<br>';
echo $product->getSpecialPrice() . '<br>';
echo $product->getTypeId() . '<br>';
echo $product->getProductUrl() . '<br>';
Using Object Manager
Here is the example code to get the product information by product id and SKU in Magento 2 using object manager.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
// get product by product id
$product = $productRepository->getById(15);
// get product by product sku
$product = $productRepository->get('MT12');
echo $product->getEntityId() . '<br>';
echo $product->getName() . '<br>';
echo $product->getSKU() . '<br>';
echo $product->getPrice() . '<br>';
echo $product->getSpecialPrice() . '<br>';
echo $product->getTypeId() . '<br>';
echo $product->getProductUrl() . '<br>';
Use Magento\Catalog\Api\ProductRepositoryInterface to get it in your constructor.? What shall I do? Thanks a lot – davideghz Dec 16 '16 at 17:17repository->getList()not a 'model call' whilerepository->get()is? seems like they are both methods on the same object. – Shawn Northrop Jan 18 '18 at 18:56\Magento\Catalog\Api\ProductRepositoryInterface? This is an interface rather than an actual class. I'm new to Magento but I would have thought to injectMagento\Catalog\Model\ProductRepositorywhich implements the interface. Also, through my digging around / learning I have seen people Injecting*Factoriesrather than*Repositories, i.e\Magento\Catalog\Model\ResourceModel\Product\CollectionFactoryand then creating a collection and adding Filters or whatnot. Should this be avoided? how would you get a Collection with aRepository? – Shawn Northrop Jan 18 '18 at 19:03