Programmatically change Magento’s core config data

Programmatically change Magento’s core config data

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:

/*
*turns notice on
*/
Mage::getConfig()->saveConfig('design/head/demonotice', '1', 'default', 0);
/*
*turns notice off
*/
Mage::getConfig()->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!

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 36

How To Connect Google Analytics 4 To Magento 2

Magento Grid Serializer for Ajax Grids Damir Korpar
Damir Korpar, | 10

Magento Grid Serializer for Ajax Grids

Programmatically add a custom block in Magento Admin Kristijan Glibo
Kristijan Glibo, | 4

Programmatically add a custom block in Magento Admin

23 comments

  1. For magento 2 you can use the following example:
    (also, use factories, but I digress)

    public function __construct(
    \Magento\Config\Model\ResourceModel\Config $resourceConfig
    ) {
    $this->_resourceConfig = $resourceConfig;
    }

    public function doThings(path, $value, $scope, $scopeId){
    // $path, $value, $scope, $scopeId correspond to column data in the core_config_data table
    $this->_resourceConfig->saveConfig($path, $value, $scope, $scopeId)
    }
    {

    }
    </code

  2. when i test on localhost all functions are working fine but when i upload on live server save config button are not working for Payment Method tab Please suggest how to solve this issue.

  3. Below code is also useful for enter data in core config data
    Mage::getModel(‘core/config’)->saveConfig(‘my/path/whatever’, ‘Some New Value’;

  4. I have fixed a bug that was not related to this post just because you used images. I have been searching for almost 8 hrs. Thanks.

  5. Hi guys,
    I’m trying to create an API that will list me all settings from one Group. Could anybody give me a hind how to list this? Thanks.

    1. To be more clear, I don’t need help on creating an api. I’m just looking for a way to list the settings from a certain group.

    2. Ok, I will answer this myself.

      Mage::getStoreConfig(section_name);

      And i get the settings in that section. Problem is that I want to list all sections available in one tab. Is there any way of doing that?

  6. Thanks Just setup functionality this is working & you can add your module aslo this is working

  7. @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.

  8. 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

  9. 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();
  10. 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

  11. Hi,

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

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

  12. @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 😉

  13. @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);
  14. 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()

  15. @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.

  16. 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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.