Magento – How to use registry

  • mage::registry
  • mage::register
  • magento registry
  • Mage registry
  • magento Mage::register

Registry is very useful class/utility in Magento. Magento registry allow you to save/register or create variable which you can see or access anywhere in your module/code.

Magento also use this to save current product  or current category used/accessed. The registry variable acts as a global variable (based on session)

We register a variable with register() function.
We can unregister the variable with unregister()function.
To fetch the registry variable, we use registry()function.
We can store anything in the registry variable. It can be integer, string, array, etc.

Looking for improvement for your Magento Store?
Facebook Connector Extension
Product related article extension
Magento Offer popup on exit extension
Unlimited Slideshow Magento Extension
Light Checkout in One Step Magento Extension

In the example below, I will be showing you how to create, fetch and destroy registry variable. In other words, I mean how to register, fetch and unregister variables in Magento.

Here, I will be registering store id (integer). The variable key will be ‘store_id‘.

Mage::register('store_id', Mage::app()->getStore()->getId());

Mage::app()->getStore()->getId() gives the current store id.

The key of our newly created variable is ‘store_id’. To fetch the newly registered variable, we will use the following code:

Mage::registry('store_id');

We will destroy/unregister the variable when we no longer need it.

Mage::unregister('store_id');

Hope this helps and thanks for reading :

If you have followed this but it doesn’t work. Please check your variable name – it might be same a registry name Magento have used.

Comments

Leave a Reply

Your email address will not be published.

4 × 3 =

Security Code: