Magento: How to set and get registry?
Registry means registering or creating a new variable which is to be used later on in the project/code. The registry variable acts as a global variable which can be used anywhere.
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.
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 :-)


Hi Mukesh,
thanks for this, really useful!
One thing that keeps popping into my mind is, is this registry variable available only to one user or to all users that are currently on page (to several users).
What I do is I set registry var for one user and later I empty it. I’m just wondering if in between some other user gets this var or even overwrite this var..
Thanks for useful data throughout your blog!
cheers
Leave your response!