We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Get Current Product In Magento 2?
To get the current product in Magento 2, you can use dependency injection to inject the
Magento\Catalog\Model\ProductRepository
and Magento\Framework\Registry
classes in your custom block, controller, or helper class.
Here’s an example of how to get the current product in a block:
1. Using Registry:
You can use theMagento\Framework\Registry
class to get the current product. The current_product
key in the registry contains the current product.
<?php namespace Vendor\Module\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\Registry; class CustomBlock extends Template { protected $registry; public function __construct( Template\Context $context, Registry $registry, array $data = [] ) { $this->registry = $registry; parent::__construct($context, $data); } public function getCurrentProduct() { return $this->registry->registry('current_product'); } }And now you can use getCurrentProduct() method:
// print current product data echo $currentProduct->getId() . '<br />'; echo $currentProduct->getName() . '<br />'; echo $currentProduct->getSku() . '<br />'; echo $currentProduct->getFinalPrice() . '<br />'; echo $currentProduct->getProductUrl() . '<br />';
2. Using ProductRepository:
Alternatively, you can use theMagento\Catalog\Model\ProductRepository
class to load the product by its ID.
<?php namespace Vendor\Module\Block; use Magento\Framework\View\Element\Template; use Magento\Catalog\Api\ProductRepositoryInterface; class CustomBlock extends Template { protected $productRepository; public function __construct( Template\Context $context, ProductRepositoryInterface $productRepository, array $data = [] ) { $this->productRepository = $productRepository; parent::__construct($context, $data); } public function getCurrentProduct() { $productId = $this->getRequest()->getParam('id'); return $this->productRepository->getById($productId); } }
3. In a Controller:
If you want to get the current product in a controller, you can similarly use the ProductRepository:<?php namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Catalog\Api\ProductRepositoryInterface; class Index extends Action { protected $productRepository; public function __construct( Context $context, ProductRepositoryInterface $productRepository ) { $this->productRepository = $productRepository; parent::__construct($context); } public function execute() { $productId = $this->getRequest()->getParam('id'); $product = $this->productRepository->getById($productId); // Now you can use $product for your logic } }
Notes:
- Registry Method: This method is generally used in product pages where the product is already loaded.
- ProductRepository Method: This method is more versatile and can be used in any context where you have the product ID.