Sometimes in Magento I need to store a value for retrieval later but I don’t want to create a new table just for one or two settings. This is good for setting flags, storing settings or for small text blurbs.
By storing the value in core_config_data in a custom path, a variable can be re-written and retrieved as much as needed. It’s a very simple process and I’ve found it be incredibly useful.
Create the new config path:
Replace “path/to/your/variable” with your own path and put this in a file. Run it once to create your custom path with the initial value of “value to store“. Alternatively, you can run the $query directly in mysql.
<?php $resource = Mage::getSingleton('core/resource'); $writeConnection = $resource->getConnection('core_write'); $query = "INSERT INTO `core_config_data` ( `config_id` , `scope` , `scope_id` , `path` , `value` ) VALUES ( NULL , 'default', '0', 'path/to/your/variable', 'value to store')"; $writeConnection->query($query); echo 'Added.';
Save data to your new config path:
$value = "123"; Mage::getModel('core/config')->saveConfig('path/to/your/variable', $value);
Read data from your new config path:
$value = Mage::getStoreConfig('path/to/your/variable'); echo $value;