Home » Magento

Magento: How to set and get registry?

18 February 2010 7,818 views Popularity: 16% Share/Bookmark

email

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

Related posts:

  1. Magento: Get store information
  2. Magento: Get list of all Categories
  3. Magento: How to get most viewed products?
  4. jQuery: Grey out background and preview image as popup
  5. Magento: Payment method not displayed in Multiple Shipping Adresses Checkout
  6. Magento: Send Transactional Email
  7. Magento: Get current and parent category
  8. Magento: Load store specific product
  9. Magento: Set Random Order in Collection using RAND()
  10. Magento: Join, filter, select and sort attributes, fields and tables
  • Kat

    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

  • kiddo

    thanks for the tut . it helped

  • kiddo

    thanks for the tut . it helped

  • Pranay

     Hi Mukesh,

    Thanks for guiding  in very simple way…thats gr8.

  • AnyDog

    Hi,
    I stumbled to this post while searching solution to this – geting layout block top.links into WordPress and fetching locale/store view using WP (multilingual enabled) variable (if $wp_lang = en …etc).Can this code be applied to my piece of code to get top.links block in different store view (language) ?Here’s my code:$layout = Mage::app()->getLayout();$layout->getUpdate() ->addHandle(‘default’) ->load();$layout->generateXml()->generateBlocks();$topLinks = $layout->getBlock(‘top.links’)->toHtml();echo $topLinks;

    Thank you