ECommerce Insights Blog

Stay on top of it all and get ahead with useful articles, how-tos, tips and tricks on e-commerce.

Definitive Guide On Magento Registry

Magento Backup PLUS - Cloud Backup Magento Extension -5450

Magento registry is a great way of sharing information anywhere in your Magento store as they are static function. In other words Magento registry implies creation of new global variables which can be accessed anywhere within your Magento store. Being a static function Magento registry can be called directly without the object being instantiated as in case of dynamic functions. Dynamic functions cannot be called without their object being instantiated that’s where Magento registry (static function) comes handy. Magento registry can be called like ClassName::StaticFunctionName()

Magento registry has two basic functions which are used to register or un-register your variables for later use.

  1. register() – this registers your variables which can be integer, string or an array.
  2. unregister() – unregisters/destroys your registered variable.
  3. registry() – used to call the registered variable.

How To Register Data in Magento Registry

Registering your variable (data) in Magento registry is easy, you need to call register() function. Let’s take few examples of registering different types of variables.

i. Register an integer store id to a variable

     Mage::register('store_id', Mage::app()->getStore()->getId());
     // Mage::app()->getStore()->getId()); gives you the store id.
    Mage::registry('store_id'); // registers store_id  variable.
    Mage::unregister('store_id'); // unregisters store_id variable

ii. Registers product id

    // Mage::register($key, $value);
    $productId = 10; // This should be product ID available in your store
    $_myproductID = Mage::getModel('catalog/product')->load($productId);
    Mage::register('myproductID', $_ myproductID);

How To Retrieve Product ID from Magento Registry

Retrieving variables from Magento registry is easy, just take a look at the following code

    // $value = Mage::registry($key)
    $_myproductID = Mage::registry('myproductID')

One of the most beautiful things about registry is widely seen in Magento code. You must be thinking that when Magento code calls $_product = $this->getProduct() it might be calling a function which in turns runs some database operations to give your product details. The beauty of Magento registry lies underneath this code. Magento doesn’t call a database function but checks and retrieves registry values which is faster than database queries. Take a look at how we have retrieved $_myproductID value from the registry. Magento uses same technique for faster responses.

Similary Magento registry can give you appropriate details without the need of calling database for many operations like

Find out current CMS page model

    $_thispage = Mage::registry('cms_page');
    echo $_thispage->getTitle().'
'; echo $_thispage->getContent();

Access current Category Model

    $_category = Mage::registry('current_category');
    echo $_category->getName();

How To View Magento Registry Contents

If for reasons you wish to see what’s written in Magento registry then there is no easy solution. You need to modify Mage.php (found under app/) to view registry content. Take back-up of Mage.php file which should be restored when you are done looking up Magento registry.

Mage.php contains lots of static functions which can be called without instantiating the class.
Open Mage.php file and add the following line anywhere within Mage.php which will eventually return the entire Magento registry

public static function getRegistry() { return self::$_registry; }

Now, you have the entire registry as an array in $entireregistryArray. You can display the entire registry with either


Or, with key’s,their data types and values/class names like this

 0): ?>
	

Store’s Current Registry

    $value): ?>
  • Registry[''] = () Array

When you run this piece of code it will give you output in the following format


• Registry['original_include_path'] = (string) .:/Applications/MAMP/bin/php5/lib/php
• Registry['_singleton/core/resource'] = (object) Mage_Core_Model_Resource

I think this is enough for you to understand Magento Registry. I would love to hear your experiences of working with Magento registry. Please leave me a comment and let me know. Subscribe our RSS to receive latest Magento updates.