Magento: get and set variables in config.xml

Written by Kevin Gimbel on , 🍿 3 min. read

The other day I had a kind of easy problem in Magento that still took me quite some time to solve. In the shop I'm working on we had a contact form module that sents and validates emails which worked perfectly fine on one page (the one it was made for), however this form should be "cloned" to be present on 4 pages with different email addresses. Since Magentos Core is MVC-like I believed there would be an easy way to pass email addresses to the Controller. Indeed I found a way to do so, and I'm happy if someone finds a better way!

I managed to set the different emails in the config.xml (located in /mymodule/etc/config.xml) like so

 # ... Default Block within the <config> block
# ....
# Example of a Router config,
# these are important for the switch below

This way the variables can be easily changed inside the config.xml, later they are retrieved (in the fronend or inside the Controller.php) with this code

	$email = Mage::getStoreConfig('email/contact');
$email = Mage::getStoreConfig('email/support');
$email = Mage::getStoreConfig('email/yetanotheremail');

Those store configs are only accessable from within this Model and its View, so in compare.phtml this would throw an error. Next thing I needed was a switch() to determine in what view I am. Again, I'm pretty sure there is a better way of handling this.

 /* This returns the name set for the Router in the config.xml.
* (Between <routers> and </routers>

$currentRoute = Mage::app()->getFrontController()->getRequest()->getRouterName();

switch($currentRoute) {
case "company_contactform_productsupport":
$email = Mage::getStoreConfig('email/productsupport');

case "comapny_contactform_contact":
$email = Mage::getStoreConfig('email/contact');

case "company_contactform_support":
$email = Mage::getStoreConfig('email/support');

$email = Mage::getStoreConfig('email/fallback');

The fallback is used for the case of no match - it should not happen but it still can happen, so it's always save to have a fallback. In this case it just goes to the generic info eMail of the client.

I'm not sure if there's a better or easier way but I found that this solution fits my needs pretty good.

Hi, I'm Kevin!

I'm a DevOps Engineer with a passion for on automation and monitoring. Before shifting into DevOps and cloud computing I worked as Front-End Developer, which is still a hobby and field of interest for me.

Picture of Kevin Gimbel, in a tiny mirror

I'm very passionated about a variety of games - digital, boardgames, and pen & paper; and also interested in Sci-Fi, Cyberpunk, and dystopian books. You can find out more on the about page.