如何在Magento 1.6.2中添加新的州/省和城市下拉列表?当我们选择美国作为国家时,我们会找到州/省的下拉列表。但并非所有国家都可以使用。
我想要的是 - 如果我选择一个国家(例如爱尔兰),它将在该国家的下拉列表中显示所有州/省,如果我选择州/省,它将显示所有城市的下拉列表那个州/省。我们怎么做呢。
我找到了一个在线添加州/省部分的链接。但是没有城市的线索。这里是链接http://www.manojyadav.co.in/magento/adding-stateprovince-india-sql-magento/
如果我能从你那里得到一个文件,我可以用自己的文件取代州城市,这将是一个很大的帮助。
答案 0 :(得分:2)
如果您想将城市配置为下拉选项,请按照以下步骤操作:
的步骤强>
假设已经创建了MagePsycho_Citydropdown
骨架模块
的 1。添加填充城市的功能。
档案:app/code/local/MagePsycho/Citydropdown/Helper/Data.php
代码:
<?php
/**
* @category MagePsycho
* @package MagePsycho_Citydropdown
* @author magepsycho@gmail.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class MagePsycho_Citydropdown_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getUaeCities()
{
$helper = Mage::helper('directory');
$cities = array(
$helper->__('Abu Dhabi'),
$helper->__('Ajman'),
$helper->__('Al Ain'),
$helper->__('Dubai'),
$helper->__('Fujairah'),
$helper->__('Ras al Khaimah'),
$helper->__('Sharjah'),
$helper->__('Umm al Quwain'),
);
return $cities;
}
public function getUaeCitiesAsDropdown($selectedCity = '')
{
$cities = $this->getUaeCities();
$options = '';
foreach($cities as $city){
$isSelected = $selectedCity == $city ? ' selected="selected"' : null;
$options .= '<option value="' . $city . '"' . $isSelected . '>' . $city . '</option>';
}
return $options;
}
}
备注:您还可以在db表中填充城市,使其更具动态性。例如,您可以创建以下类似于区域的表:
+ directory_country_city(city_id,country_id,code,default_name)
+ directory_country_city_name(locale,city_id,name)
<强> 2。使用javascript将输入文本城市字段替换为下拉列表
2.1
档案:app/design/frontend/[package]/[theme]/template/checkout/onepage/billing.phtml
代码:将以下代码添加到上述模板的最后一个
<script type="text/javascript">
<?php
$helper = Mage::helper('citydropdown');
$address = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
$defaultCity = $address->getCity();
$citiesOptions = addslashes($helper->getUaeCitiesAsDropdown($defaultCity));
?>
var billingCity = '<?php echo $defaultCity ; ?>';
function billingSwitchCityField(){
var selectVal = jQuery('#billing\\:country_id option:selected').val();
if(selectVal == "AE"){
jQuery("#billing\\:city")
.replaceWith('<select id="billing:city" name="billing[city]" class="required-entry">' +
'<option value=""></option>' +
'<?php echo $citiesOptions; ?>' +
'</select>');
}else{
jQuery("#billing\\:city")
.replaceWith('<input type="text" class=" input-text required-entry absolute-advice " title="City" value="' + billingCity + '" id="billing:city" name="billing[city]" autocomplete="off">');
}
}
jQuery(document).ready(function(){
billingSwitchCityField();
jQuery('#billing\\:country_id').change(function() {
billingSwitchCityField();
});
})
</script>
2.2
档案:app/design/frontend/[package]/[theme]/template/checkout/onepage/shipping.phtml
代码:将以下代码添加到上述模板的最后一个
<script type="text/javascript">
<?php
$helper = Mage::helper('citydropdown');
$address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
$defaultCity = $address->getCity();
$citiesOptions = addslashes($helper->getUaeCitiesAsDropdown($defaultCity));
?>
var shippingCity = '<?php echo $defaultCity ; ?>';
function shippingSwitchCityField(){
var selectVal = jQuery('#shipping\\:country_id option:selected').val();
if(selectVal == "AE"){
jQuery("#shipping\\:city")
.replaceWith('<select id="shipping:city" name="shipping[city]" class="required-entry">' +
'<option value=""></option>' +
'<?php echo $citiesOptions; ?>' +
'</select>');
}else{
jQuery("#shipping\\:city")
.replaceWith('<input type="text" class=" input-text required-entry absolute-advice " title="City" value="' + shippingCity + '" id="shipping:city" name="shipping[city]" autocomplete="off">');
}
}
jQuery(document).ready(function(){
shippingSwitchCityField();
jQuery('#shipping\\:country_id').change(function() {
shippingSwitchCityField();
});
})
</script>
以类似的方式,您也可以为其他国家/地区配置城市选项。以类似的方式,您也可以为其他国家/地区配置城市选项。 欢呼声。
答案 1 :(得分:2)
如果您查看数据文件夹中的Magento模块“Directory”:data / directory_setup / data-install-1.6.0.0.php文件,您可以看到Magento如何填满表格。
例如,要添加澳大利亚州,首先,使用这些文件创建一个模块:
应用/代码/本地/包装/ Ausregions的/ etc / config.xml中强>
<?xml version="1.0"?><config><modules>
<Package_Ausregions>
<version>0.1.0</version>
</Package_Ausregions>
</modules>
<global>
<helpers>
<ausregions>
<class>Package_Ausregions_Helper</class>
</ausregions>
</helpers>
<resources>
<ausregions_setup>
<setup>
<module>Package_Ausregions</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</ausregions_setup>
<ausregions_write>
<connection>
<use>core_write</use>
</connection>
</ausregions_write>
<ausregions_read>
<connection>
<use>core_read</use>
</connection>
</ausregions_read>
</resources>
</global>
<global>
<helpers>
<ausregions>
<class>Package_Ausregions_Helper</class>
</ausregions>
</helpers>
<resources>
<ausregions_setup>
<setup>
<module>Package_Ausregions</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</ausregions_setup>
<ausregions_write>
<connection>
<use>core_write</use>
</connection>
</ausregions_write>
<ausregions_read>
<connection>
<use>core_read</use>
</connection>
</ausregions_read>
</resources>
</global>
然后您需要编写助手类: Package / Ausregions / Helper / Data.php
class Package_Ausregions_Helper_Data extends Mage_Core_Helper_Abstract
{
最后,将您的数据安装文件添加到: Package / Ausregions / data / ausregions_setup / data-install-0.1.0.php ,其中包含以下内容:
}
$installer = $this;
$data = array(
array('AU', 'ACT', 'Australian Capital Territory'), ); foreach ($data as $row)
{
array('AU', 'NSW', 'New South Wales'),
array('AU', 'NT', 'Northern Territory'),
array('AU', 'QLD', 'Queensland'),
array('AU', 'SA', 'South Australia'),
array('AU', 'TAS', 'Tasmania'),
array('AU', 'VIC', 'Victoria'),
array('AU', 'WA', 'Western Australia')
$bind = array(
'country_id' => $row[0],
'code' => $row[1],
'default_name' => $row[2],
);
//First add data into "directory_country_region" table
$installer->getConnection()->insert($installer->getTable('directory/country_region'), $bind);
//Then get the last inserted ID for the regionID
$regionId = $installer->getConnection()->lastInsertId($installer->getTable('directory/country_region'));
$bind = array(
'locale' => 'en_US',
'region_id' => $regionId,
'name' => $row[2]
);
//Insert data into "directory_country_region_name" table
$installer->getConnection()->insert($installer->getTable('directory/country_region_name'), $bind);
这将添加状态,当您在结帐页面上时,如果您选择国家澳大利亚,它将填充插入状态的“状态”下拉列表。
答案 2 :(得分:-1)
看看Aitoc Checkout Fields Manager。它可以让您在不必编写新模块的情况下添加该字段,并且可能比雇用某人编写自定义作业更便宜。