Magento:设置刚创建的网站的配置值? [英] Magento: set config values of just created website?
问题描述
我正在以编程方式创建网站/用户等...
I'm programmtically creating websites/users etc ...
问题出在这里:创建网站时,我之后无法立即设置配置值.
Here's the problem: When creating a website, I can't immediatly set the config values afterwards.
代码:
<?php
/* Website information */
$website_data = array(
'name' => 'Company name',
'code' => 'website_company_1',
'sort_order' => '1',
);
/* Save website */
$website = Mage::getModel('core/website');
$website->setData($website_data);
$website->save();
/* Get website code */
$web_code = $website->getCode();
/* R-int stores */
Mage::app()->reinitStores();
/* Config data array example */
$data = array('name' => 'Company 1', 'phone' => '056 22 33 61')
/* Set config values in array */
$groups = array();
foreach($data as $key => $value){
$groups['store_information']['fields'][$key]['value'] = $value;
}
/* Save config values */
Mage::getModel('adminhtml/config_data')
->setSection('general')
->setWebsite($web_code)
->setStore(NULL)
->setGroups($groups)
->save();
/* Re-init again */
Mage::app()->reinitStores();
但是,由于某些原因,该方法不起作用,但是如果我首先创建一个网站(使用相同的代码),然后再执行此config-save功能,则可以正常工作.好像它需要设置/更新配置值之前先需要新页面加载一样.我以为重新初始化可以解决这个问题,但事实并非如此……
However, this doesn't work for some reason, but if I create a website first(with the same code), then execute this config-save function afterwards, it works fine. As if it needs a new page load first before it can set/update config values. I thought the re-init would solve this but it doesn't ...
有想法吗?
推荐答案
您没有提及您所使用的Magento版本.我已经在1.4.1.1上测试了以下内容,并进行了一些更改,因此这是一个正在运行的示例.
You didn't mention which Magento version you are on. I have tested the below on 1.4.1.1 and made some changes so it is a running example.
主要区别是
Mage::app()->reinitStores();
到
Mage::app()->getConfig()->reinit();
重新加载配置,同时重新加载缓存.
which re-loads the config while also reloading the cache.
完整示例:
<?php
require_once 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
Mage::app();
/* Website information */
$website_data = array(
'name' => 'Website Name',
'code' => 'website_company',
'sort_order' => '2',
'is_active' => 1,
);
/* Save website */
$website = Mage::getModel('core/website');
$website->setData($website_data);
$website->save()->load();
/* Save store */
$storeGroup = Mage::getModel('core/store_group');
$storeGroup->setData(
array(
'root_category_id' => '3',
'website_id' => $website->getId(),
'name' => 'Store',
)
);
$storeGroup->save()->load();
$store = Mage::getModel('core/store');
$store->setData(
array(
'website_id' => $website->getId(),
'name' => $storeGroup->getName(),
'code' => 'store_' . $website->load()->getId(),
'group_id' => $storeGroup->getGroupId(),
'is_active' => 1,
)
);
$store->save()->load();
/* Re-init */
Mage::app()->getConfig()->reinit();
/* Config data array example */
$data = array('name' => 'Company 1', 'phone' => '056 22 33 61');
/* Set config values in array */
$groups = array();
foreach ($data as $key => $value) {
$groups['store_information']['fields'][$key]['value'] = $value;
}
/* Save config values */
$data = Mage::getModel('adminhtml/config_data')
->setSection('general')
->setWebsite($website->getCode())
->setGroups($groups)
->save();
这篇关于Magento:设置刚创建的网站的配置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!