I've noticed that Magento keeps the URL to the logo it uses for each store you setup in the core_config_data
table.
If I run this SQL:
SELECT *
FROM core_config_data`
WHERE path = 'design/header/logo_src'
I get a list of stores and their associated logo. I also get a config_id
and a scope_id
.
I'd like to be able to update these logo's programmatically behind the scenes but I can't figure out how to relate this ta开发者_如何学Cble's data back to a store name.
config_id
and store_id
got to somehow link back to another table that sets up the relationship. Magento's EAV model, ugh :)
Any ideas?
Magento provides an API to set and retrieve configuration values. I wrote an article about fetching data out of the config. You'll want to review it if any of the terminology below confuses you.
As for setting the variables, it's not quite as simple as saying "I want to set the design/header/logo_src" variable to "this value". You also need to specify which website you're saving the values for, and which store you're saving the values for.
It's important to be careful here. I haven't looked deeply at the system to see if Magento is enforcing the "show for website, show for site" granularity of the config values at the back-end/resource level, or only at the UI level. My instincts say it's the later, which mean you might end up saving a value at the website/store level that wasn't intended to be savable at that level, and that could lead to the dreaded Undefined Behavior.
In the 1.4 community edition you can see where Magento is saving the config values at the following location
File: app/code/core/Mage/Adminhtml/controllers/System/ConfigController.php
//around line 126
Mage::getModel('adminhtml/config_data')
->setSection($section)
->setWebsite($website)
->setStore($store)
->setGroups($groups)
->save();
Caveats out of the way, the code for you to update just the logo for the top level store/website config would be.
//create a groups array that has the value we want at the rigth location
$groups_value = array();
$groups_value['header']['fields']['logo_src']['value'] = 'images/logo.gif';
Mage::getModel('adminhtml/config_data')
->setSection('design')
->setWebsite(null)
->setStore(null)
->setGroups($groups_value)
->save();
The line
Mage::getModel('adminhtml/config_data')
instantiates a Magento model.
The 'design'
in the setSection
method as well as the 'header'
and 'logo_src'
string correspond to the config URI
design/header/logo_src
I'm pretty sure the null
s passed to setWebsite
and setStore
tell the object you're saving this value at the top level. You could also pass in store and website IDs. (I'm a little shaky on that, but when you save the logo normally that's what the system is doing)
The 'fields'
and 'values'
are hard coded. The intent of the object is to save multiple config values at once from all the groups in a section.
Checkout the saveAction
method in the above controller if you're curious.
The most easiest and shortest method is:
Mage::getConfig()->saveConfig('design/header/logo_src', $logo);
Thanks for the in-depth reply. I truly appreciate it.
I figured out that I can just insert a new record like this:
INSERT INTO core_config_data (`scope`, `scope_id`, `path`, `value`)
VALUES ('websites', $websiteId, 'design/header/logo_src', $logo);
I tested it and it appears to add a different logo for the store in the configuration - design page, but when i reference the logo by <?php echo $this->getLogoSrc() ?>
in my header.phtml it is still pulling the default logo from the top level configuration.
I think that using your post as a guideline I can start debugging using Mage::log()
and see where it takes me.
精彩评论