Magento Themes and Magento Extensions

Programmatically change Magento’s core config data

Share

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…
Programmatically change Magento’s core config data

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

1
2
3
4
5
6
7
8
9
$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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
}
.
.
.
}

Leave a Reply

You must be logged in to post a comment.