Magento get current product

Magento get current product

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento get current product

Magento: Retrieving the Current Product ID

Hello developers! Today we’re going to tackle an intriguing issue that’s been puzzling some of you - retrieving the current product id on a product view page in Magento 1.7.0.2 with Varnish cache enabled.

The Challenge at Hand

You’ve created a custom, uncached block and are trying to get the current product id using Mage::registry('current_product')->getId() or $this->getProduct()->getId(). However, it seems these methods aren’t cooperating as expected. You’re receiving a “Call to a member function getId() on a non-object” error.

Let’s dive into why this might be happening and how we can resolve it.

Understanding the Magento Registry

In Magento, the Mage::registry() function is used to store and retrieve objects across multiple requests or actions. It’s particularly useful when you need to access an object that was created in one action and then used later in another.

However, there are a few key points to remember:

  1. Context Matters: The registry is context-specific. An object registered in the frontend context won’t be available in the adminhtml context unless explicitly set.

  2. Lifespan: Objects stored in the registry have a lifespan tied to the scope they were added to. Once that scope ends, objects are automatically removed from the registry.

The Product Registry in Magento

When you’re on a product view page, Magento doesn’t register the current product by default in the current_product key under the frontend context. This is why Mage::registry('current_product') returns null and causes the fatal error.

So what’s going wrong with your block?

The Issue with Your Custom Block

You’re creating a new block of type “catalog/product_view”, which corresponds to the standard product view block. However, this doesn’t register a ‘current_product’ in the registry by default.

To understand why $this->getProduct()->getId() isn’t working, let’s look at what getProduct() actually does:

public function getProduct()
{
    if (!$this->hasProduct()) {
        $productId = $this->getRequest()->getParam('id');
        if ($productId) {
            $product = Mage::registry('current_product');
            if ($product) {
                return $product;
            }
        }

        $product = new Varien_Object();
    }

    return $product;
}

As you can see, getProduct() first checks if a product is already registered in the ‘current_product’ registry. If not, it tries to retrieve the product id from the request parameter and then registers that as ‘current_product’. However, since no product is registered in your case, $this->getProduct() returns a blank object (new Varien_Object()), which doesn’t have a getId method.

The Solution

So how do we get around this?

The key lies in making sure the current product is properly registered before trying to access it. You can achieve this by modifying your block’s initialization:

public function __construct()
{
    if (!Mage::registry('current_product') && Mage::getSingleton('catalog/session')->getData('product')) {
        Mage::register('current_product', Mage::getSingleton('catalog/session')->getData('product'));
    }
}

In this modified constructor, we check if the ‘current_product’ isn’t already registered. If not, we try to retrieve the current product from the catalog/session and register it as ‘current_product’.

After implementing this change, your custom block should be able to correctly access the current product using either Mage::registry('current_product')->getId() or $this->getProduct()->getId().

Conclusion

Retrieving the current product in Magento isn’t always straightforward due to its complex registry system and context-specific nature. However, by understanding how Magento registers products and modifying our custom block’s initialization accordingly, we can successfully access the current product on a product view page.

Remember, when working with the Magento registry, it’s crucial to understand the context and lifespan of objects. Always ensure you’re registering your objects in the correct context for your needs.

I hope this guide has shed some light on this issue and provided a practical solution that will help you in your development journey!

If you found this post helpful, feel free to explore more Magento tutorials and best practices at Laravel Company. We’re always eager to share our knowledge and help fellow developers master the intricacies of e-commerce platforms like Magento.

Happy coding!

Magento 1 Catalog Cache Themes

Our network