Magento添加中国省份列表

piaoling  2011-07-26 16:16:05

第一步:找到Directory模块,在sql下创建新的升级脚本,版本号只要比之前的大就好,例如我的是mysql4-upgrade-0.8.10-0.8.11.php。

 

文件内容:

< ?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Directory
 * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 
/** @var Mage_Core_Model_Resource_Setup */
$installer = $this;
 
/** @var Varien_Db_Adapter_Pdo_Mysql */
$connection   = $this->getConnection();
$regionTable  = $installer->getTable('directory/country_region');
$regNameTable = $installer->getTable('directory/country_region_name');
 
$regionsToIns = array(
    array('CN', 'GD', '广东'),
	array('CN', 'BJ', '北京'),
	array('CN', 'SH', '上海'),
	array('CN', 'TJ', '天津'),
	array('CN', 'HE', '河北'),
	array('CN', 'SX', '山西'),
	array('CN', 'NM', '内蒙古'),
	array('CN', 'LN', '辽宁'),
	array('CN', 'JL', '吉林'),
	array('CN', 'HL', '黑龙江'),
	array('CN', 'JS', '江苏'),
	array('CN', 'ZJ', '浙江'),
	array('CN', 'AH', '安徽'),
	array('CN', 'FJ', '福建'),
	array('CN', 'JX', '江西'),
	array('CN', 'SD', '山东'),
	array('CN', 'HA', '河南'),
	array('CN', 'HB', '湖北'),
	array('CN', 'HN', '湖南'),
	array('CN', 'GX', '广西'),
	array('CN', 'HI', '海南'),
	array('CN', 'CQ', '重庆'),
	array('CN', 'SC', '四川'),
	array('CN', 'GZ', '贵州'),
	array('CN', 'YN', '云南'),
	array('CN', 'XZ', '西藏'),
	array('CN', 'SN', '陕西'),
	array('CN', 'GS', '甘肃'),
	array('CN', 'QH', '青海'),
	array('CN', 'NX', '宁夏'),
	array('CN', 'XJ', '新疆'),
	array('CN', 'HK', '香港'),
	array('CN', 'AM', '澳门'),
	array('CN', 'TW', '台湾'),
);
 
foreach ($regionsToIns as $row) {
    if (! ($connection->fetchOne("SELECT 1 FROM `{$regionTable}` WHERE `country_id` = :country_id && `code` = :code", 
array('country_id' => $row[0], 'code' => $row[1])))) {
        $connection->insert($regionTable, array(
            'country_id'   => $row[0],
            'code'         => $row[1],
            'default_name' => $row[2]
        ));
 
        $regionId = $connection->fetchOne("SELECT `region_id` FROM `{$regionTable}` WHERE `country_id` = :country_id && `code` = :code",
 array('country_id' => $row[0], 'code' => $row[1]));
 
        if ($regionId && ! $connection->fetchOne("SELECT 1 FROM `{$regNameTable}` WHERE `region_id` = {$regionId}")) {
	        $connection->insert($regNameTable, array(
	            'locale'    => 'zh_CN',
	            'region_id' => $regionId,
	            'name'      => $row[2]
	        ));
	    }
    }
}

第二步:修改etc下的config.xml中的版本号,将0.8.10改为0.8.11,保存。

第三步,去后台的缓存管理中清除Magento的缓存,搞定。

类别 :  magento(258)  |  浏览(4223)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: