如何在magento中以编程方式创建站点,存储和查看

时间:2011-11-29 10:16:09

标签: magento

我正在尝试以编程方式创建网站,存储和查看。我找到了这段代码:

$websiteModel = Mage::getModel('core/website');

$postData = array();
$postData['website']['name'] = 'Store Name';
$postData['website']['code'] = 'store_name';
$postData['website']['sort_order'] = '';
//$postData['website']['is_default'] = '';
$postData['website']['website_id'] = '';

$websiteModel->setData($postData['website']);

这样的事情会起作用吗?还有与商店和视图相关的模型是什么?感谢。

1 个答案:

答案 0 :(得分:30)

使用此代码:

//#addWebsite
    /** @var $website Mage_Core_Model_Website */
    $website = Mage::getModel('core/website');
    $website->setCode('<your_website_code_here>')
        ->setName('<your_website_name>')
        ->save();

//#addStoreGroup
    /** @var $storeGroup Mage_Core_Model_Store_Group */
    $storeGroup = Mage::getModel('core/store_group');
    $storeGroup->setWebsiteId($website->getId())
        ->setName('<your_store_name>')
        ->setRootCategoryId('<needed_root_category_id>')
        ->save();

//#addStore
    /** @var $store Mage_Core_Model_Store */
    $store = Mage::getModel('core/store');
    $store->setCode('<your_store_view_code_here>')
        ->setWebsiteId($storeGroup->getWebsiteId())
        ->setGroupId($storeGroup->getId())
        ->setName('<your_store_view_name>')
        ->setIsActive(1)
        ->save();

如果您需要从前端执行此操作,请在此代码之前添加行Mage::registry('isSecureArea');