How to Get Current Product in Magento 2?

by mageefy on Feb 16, 2023 Celebrities 416 Views

If you are working on any Magento 2 project, then many times you need to get current product object. Magento 2 has several different ways to get these data. Here we are going explain more popular ways. Magento allows you to create and configure 6 different product types in the admin panel. To get current product details you need to use Registry. The current product in Magento 2 is stored to Magento registry, with the key current_product. The current product includes the following information: name, sku, final price, url and associated category ids. Use below code snippet to get the current product.

 

Use the below code in your block file.

<?php

namespace Mageefy\Custom\Block:

class BlockClass extends \Magento\Framework\View\Element\Template
{
    protected $registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }
}

?>

 

Call function in your .phtml file:

<?php
$currentProduct = $block->getCurrentProduct();
    echo $currentProduct->getId() . '<br />';
    echo $currentProduct->getName() . '<br />';
    echo $currentProduct->getSku() . '<br />';
    echo $currentProduct->getFinalPrice() . '<br />';
    echo $currentProduct->getProductUrl() . '<br />';
    print_r ($currentProduct->getCategoryIds()) . '<br />';    
?>

https://www.mageefy.com/blog/post/how-to-get-current-product-in-magento-2

Article source: https://article-realm.com/article/Arts-Entertainment/Celebrities/37427-How-to-Get-Current-Product-in-Magento-2.html

Comments

No comments have been left here yet. Be the first who will do it.
Safety

captchaPlease input letters you see on the image.
Click on image to redraw.

Reviews

Guest

Overall Rating:

Most Viewed Articles

Statistics

Members
Members: 17272
Publishing
Articles: 79,999
Categories: 202
Online
Active Users: 142
Members: 0
Guests: 142
Bots: 2274
Visits last 24h (live): 6077
Visits last 24h (bots): 43335

Latest Comments

Your post is quite inspiring. https://gptimage26.io/ can help turn your ideas into images. Start with a text prompt or a reference image, then generate visual content and refine the composition,...
A clear, practical overview of Peacock TV activation that helps readers understand how linking their account to a television enables streaming. Following the on-screen instructions and entering...
<a href="https://aniwaves.co.uk/">Aniwave</a> is the perfect website for anyone who wants to Watch Anime Online for free. 
Aniwave is a Free Anime streaming platform where you can watch Anime in High quality with subtitles and dubbed. No need to registration. you can watch all type genres anime such as Action, drama,...
Interesting post. I Have Been wondering about this issue. so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks amanahtoto    
Great knowledge, do anyone mind merely reference back to it amanahtoto    
Very rapidly this site will be famous among all blogging and site-building users, due to it's good articles amanahtoto  
Excellent information providing by your Article thank you for taking the time to share with us such a nice article.  amanahtoto  
I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing  amanahtoto...
You have observed very interesting points ! ps decent internet site . pakde4d  

Translate To: