Home » Magento

Magento: How to set and get registry?

18 February 2010 968 views One Comment Popularity: 11% Share/Bookmark

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 :-)

From Mukesh Chapagain's Blog | Post Magento: How to set and get registry?

Related posts:

  1. Magento: Get store information
  2. Magento: Load store specific product
  3. Magento: How to get most viewed products?
  4. jQuery: Grey out background and preview image as popup
  5. Magento: Block Controller Model Helper Override

One Comment »

  • Kat said:

    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!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.