Question

I'm looking for a right way to create global variables in Magento that I can use it on every view page. I have some solution in mind but I don't if it's good enough.

  1. Create a module which provides a global configuration - I don't prefer this much since I rather work with php array rather than xml, just my stupid taste.

  2. Override the base block to have one more utility function

It's just my thought, any other suggestions are welcome :)

Was it helpful?

Solution

You could create a session variable to have a persistent state over several requests like this:

$session = Mage::getSingleton("core/session");
$session->setData("your_variable", "your value");
// ...
// Now you can retrieve your data elsewhere or even after the next request:
$session = Mage::getSingleton("core/session");
$yourVariable = $session->getData("your_variable"); // will be "your value"

You can also create your own session model, if you want to avoid the core/session namespace.

OTHER TIPS

Use the built-in Custom Variables functionality which gives you editable values from the admin:

http://www.magentocommerce.com/knowledge-base/entry/creating-custom-variables

When calling from your template:

Mage::getModel('core/variable')->loadByCode('my_custom_var')->getValue('plain');

When calling from anywhere else (CMS, emails, etc.):

{{customVar code=my_custom_var}}

Source:

https://stackoverflow.com/questions/6221054/magento-how-do-i-access-custom-variables-in-php

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top