Programmatically change Magento’s core config data

Featured Image

Every Magento installation has certain core configuration data already set. When you update those values from the administration interfaces, changes are saved mainly to core_config_data database table. It seems important and something that you shouldn’t touch, right? As always, there are times you will wish to get your hands on it. In some cases you will wish to chance settings directly from the code. This article demonstrates the proper way.


Let’s say we want to change “demo store notice” (on/off) – change value from 0 to 1 and vice versa.
All you need to do is open your database,
for example with phpmyadmin,
browse table “core_config_data“,
change the data you want and save it…

I’m just joking, that’s not the way.
Here it is, you can call it wherever in your code:

$inchooSwitch = new Mage_Core_Model_Config();
/*
*turns notice on
*/
$inchooSwitch ->saveConfig('design/head/demonotice', "1", 'default', 0);
/*
*turns notice off
*/
$inchooSwitch ->saveConfig('design/head/demonotice', "0", 'default', 0);

Code which does the magic:

class Mage_Core_Model_Config
{
	.
	.
	.
	/**
     * Save config value to DB
     *
     * @param string $path
     * @param string $value
     * @param string $scope
     * @param int $scopeId
     * @return Mage_Core_Store_Config
     */
    public function saveConfig($path, $value, $scope = 'default', $scopeId = 0)
    {
        $resource = $this->getResourceModel();
        $resource->saveConfig(rtrim($path, '/'), $value, $scope, $scopeId);
        return $this;
    }
	.
	.
	.
}

Enjoy coding!

14
Top

Care to rate this post?

Author

Vedran Subotic

Ex. Inchooer

Vedran worked in Inchoo from 2009 to 2012 as a backend developer.

Other posts from this author

Discussion 14 Comments

Add Comment
  1. m4e

    Nice.
    Why you don’t use $inchooSwitch = Mage::getModel(‘core/config’);
    Are you aware of rewrites for the model?
    It’s not a Magento-like coding, you know :)

  2. When I tried this, I had to add a clear cache statement, to get the value updated right away:

    Mage::getConfig()->cleanCache();
    
  3. @m4e As far as know Mage_Core_Model_Config is unrewritable anyway… Or do I miss something? I mean, I have never tried it, but I’m almost sure it is imposible to do it via standard way. Correct me if I’m wrong.

  4. m4e

    Tomas, I’ve never tried it before too :) You’re right, I can’t get it rewritten. Anyway it better than using ‘new’ operand, imho. Otherwise one can use Mage::getConfig()

  5. @Tomas, @m4e
    Since Magento already have instantiated config object there is no need to create new Mage_Core_Model_Config instance. Just use this one instead:

    Mage::getConfig()->saveConfig();

    But the way, if you want just change a config value only for the current session, without modifying DB, use this method:

    Mage::getConfig()->setNode($path, $value);
  6. @m4e I assumed that it can’t be rewriten since I have tried to rewrite Mage_Core_Model_Locale_Config. Fortunately I solved problem, but not by rewriting it. :)
    @Ivan Thanks for pointing that out, however I just wanted to say something about rewriting mentioned class ;)

  7. Very useful for Magento deployment. Thanks

  8. mark

    Hi,

    Can anyone tell me why “Save Config” button is not working.

    I make the changes and hit Save Config button and nothing happens.

  9. Amol

    Hi,
    Can u please tell me in which table the data get stored when i create a new email template from System – > Transactional Emails – > Add new template and assign it to Sales Email -> Order

  10. Maverick

    in order to get the data value updated, you need to add this to your code ( right after saving the data value) :

       Mage::getConfig()->reinit();
                Mage::app()->reinitStores();
    
  11. Cool! I found this post really funny at the beginning! Great solution thanks!

  12. Very helpful post and we got a laugh to boot. Thanks for sharing and to the commenters for the Mage::getConfig() suggestion.

  13. kavita

    i have create new configuration model, and data will be added in db also..nw i want to display this data on my front-end..wt to do?? reply me ASAP

  14. Walter

    @Thomas, it is possible to have a custom Mage_Core_Model_Config. Just set up the proper module, then launch magento with the specified option.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top