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 :-)
Related posts:
- Magento: Get store information
- Magento: Get list of all Categories
- Magento: How to get most viewed products?
- jQuery: Grey out background and preview image as popup
- Magento: Payment method not displayed in Multiple Shipping Adresses Checkout
- Magento: Send Transactional Email
- Magento: Get current and parent category
- Magento: Load store specific product
- Magento: Set Random Order in Collection using RAND()
- Magento: Join, filter, select and sort attributes, fields and tables
