设置多语言商店(任何sql升级)的最佳做法是什么?

时间:2011-06-17 13:13:21

标签: magento locale

手动添加商店视图很无聊,尤其是当您的商店有多种语言时。 有没有人使用sql升级脚本来添加商店视图和分配适当的区域设置? 我怎么能通过sql升级脚本来做到这一点?

3 个答案:

答案 0 :(得分:2)

创建一个自定义模块(如果您愿意,请使用Module Creator扩展名),然后将此代码添加到mysql4-install-0.1.0.php并根据需要进行修改。

$installer = $this;

$installer->startSetup();
// Start with a root category

$root_cat = Mage::getModel("catalog/category");
$root_cat->setData(array(
    "name"              => "Store Name",
    "url_key"           => "root",
    "description"       => "Store Name root category",
    "display_mode"      => Mage_Catalog_Model_Category::DM_PRODUCT,
    "default_sort_by"   => Mage::getModel("catalog/category")->getDefaultSortBy(),
    "available_sort_by" => Mage::getModel("catalog/category")->getDefaultSortBy(),
    "is_active"         => 1,
    "is_anchor"         => 0,
    "include_in_menu"   => 0,
    "parent_id"         => 1,
    "path"              => Mage::getModel("catalog/category")->load(1)->getPath(),
    "attribute_set_id"  => Mage::getModel("catalog/category")->getDefaultAttributeSetId()
));

try {
    $root_cat->save();
} catch (Exception $e) {
    Mage::logException($e->getMessage());
    return;
}

//setup an array of our websites, stores and storeviews
$website_array = array(
    "store_us"  => array(
        "name"          => "Store Name - US",
        "store"         => "Store Name - US Store",
        "store_view"    => array(
            "name"      => "English (US)",
            "code"      => "store_us_en"
        )
    ),
    "store_au"  => array(
        "name"          => "Store Name - AU",
        "store"         => "Store Name - AU Store",
        "store_view"    => array(
            "name"      => "English (AU)",
            "code"      => "store_au_en"
        )
    ),
    "store_de"  => array(
        "name"  => "Store Name - UK",
        "store" => "Store Name - UK Store",
        "store_view"    => array(
            "name"      => "UK",
            "code"      => "store_uk_en"
        )
    ),
);

$i = 1;

foreach ($website_array as $site_code => $site_details) {

    try {

        // First, the website

        $site = Mage::getModel("core/website");
        $site->setData(array(
            "code"          => $site_code,
            "name"          => $site_details["name"],
            "is_default"    => (($i == 1) ? 1 : 0),
            "sort_order"    => $i
        ));
        $site->save();

        // then attach the store to the website

        $store = Mage::getModel("core/store_group");
        $store->setData(array(
            "website_id"        => $site->getId(),
            "root_category_id"  => $root_cat->getId(),
            "name"              => $site_details["store"]
        ));
        $store->save();

        // Set this store as default for the website

        $site->setDefaultGroupId($store->getId());
        $site->save();

        // Finally, the store view

        $view = Mage::getModel("core/store");
        $view->setData(array(
            "website_id"    => $site->getId(),
            "group_id"      => $store->getId(),
            "name"          => $site_details["store_view"]["name"],
            "code"          => $site_details["store_view"]["code"],
            "sort_order"    => $i,
            "is_active"     => 1
        ));
        $view->save();

        // Set this store view as default for the store

        $store->setDefaultStoreId($view->getId());
        $store->save();


    } catch (Exception $e) {
        Mage::logException($e->getMessage());
    }

    $i++;
}

HTH,
JD

答案 1 :(得分:2)

您可以通过以下方式为商店视图设置区域设置:

$groupsValue = array();
$groupsValue['locale']['fields']['code']['value'] = 'uk_UA';

Mage::getModel('adminhtml/config_data')
    ->setSection('general')
    ->setWebsite('base')
    ->setStore($storeViewCode)
    ->setGroups($groupsValue)
    ->save();

答案 2 :(得分:2)

添加多个商店视图和分配区域设置的完整代码:

$defaultStore = Mage::getModel('core/store')->load(1);
$websiteId = $defaultStore->getWebsiteId();
$store = Mage::getModel('core/store_group')->load($defaultStore->getGroupId());

$storeViewsData = array(
    'es' => array(
        'name'   => 'Spanish',
        'locale' => 'es_ES',
    ),
    'fr' => array(
        'name'   => 'French',
        'locale' => 'fr_FR',
    ),
    'pt' => array(
        'name'   => 'Portuguese',
        'locale' => 'pt_PT',
    ),
    'it' => array(
        'name'   => 'Italian',
        'locale' => 'it_IT',
    )
    );

    foreach ($storeViewsData as $code => $data) {
    $view = Mage::getModel('core/store');
        $view->setData(array(
            'website_id'    => $websiteId,
            'group_id'      => $store->getId(),
            'name'          => $data['name'],
            'code'          => $code,
            'is_active'     => 1
        ));
        $view->save();

        Mage::getConfig()->reinit();
        Mage::app()->reinitStores();


        $groupsValue = array();
        $groupsValue['locale']['fields']['code']['value'] = $data['locale'];

        Mage::getModel('adminhtml/config_data')
            ->setSection('general')
            ->setWebsite('base')
            ->setStore($code)
            ->setGroups($groupsValue)
            ->save();
    }