How to Get Current Product in Magento 2?

by mageefy on Feb 16, 2023 Celebrities 393 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: 17130
Publishing
Articles: 79,545
Categories: 202
Online
Active Users: 11325
Members: 10
Guests: 11315
Bots: 25044
Visits last 24h (live): 13694
Visits last 24h (bots): 63339

Latest Comments

It's always fascinating to see how specific, niche markets like aluminum brazing sheets can experience such significant growth. The insights on customer demographics are particularly valuable;...
Today, we're going to explore this gaming experience using slope 2 as our main example. While it might seem different from traditional store management games at first glance, it...
The MetaMask Wallet Extension is a browser extension designed to let users store, send, receive, and manage cryptocurrencies directly from supported web browsers. It also enables seamless...
PancakeSwap Exchange is a decentralized cryptocurrency exchange built primarily on the BNB Smart Chain (BSC). Instead of using an order book like centralized exchanges, it relies on an Automated...
Strike Login is the authentication process that allows registered users to securely access their Strike account through the official mobile application. After verifying your credentials, you can...
The Rabby Wallet Extension is a browser-based cryptocurrency wallet created by the DeBank team. It supports hundreds of Ethereum Virtual Machine (EVM)-compatible blockchain networks, allowing...
The Rabby Wallet Extension is a browser-based cryptocurrency wallet created by the DeBank team. It supports hundreds of Ethereum Virtual Machine (EVM)-compatible blockchain networks, allowing...
The Rabby Wallet Extension is a browser-based cryptocurrency wallet created by the DeBank team. It supports hundreds of Ethereum Virtual Machine (EVM)-compatible blockchain networks, allowing...
If you've scrolled through social media lately, you've probably seen those colorful grids of green, yellow, and gray squares. That's Wordle, the deceptively simple word puzzle game that's captured...
This is a really detailed look at the seafood market's future! The projected growth to $178.37 billion is huge, and it's interesting to see how technology and consumer preferences are shaping...

Translate To: