如何在Magento 1.6.2中添加新的州/省和城市下拉列表?当我们选择美国作为国家/地区时,我们会找到一个州/省的下拉列表。但这并不适用于所有国家。
我想要的是-如果我选择一个国家(例如爱尔兰),它将在下拉列表中显示该国家的所有州/省,如果我选择一个州/省,它将在下拉列表中显示该州/省下的所有城市。我们怎么才能做到这一点。
我在网上找到了一个添加州/省部分的链接。但对于城市来说,没有任何线索。这里是链接http://www.manojyadav.co.in/magento/adding-stateprovince-india-sql-magento/
如果我能从你那里得到一个文件,我可以用我自己的城市替换州级城市,这将是一个很大的帮助。
发布于 2012-06-28 00:52:51
查看Aitoc Checkout字段管理器。它将允许您在无需编写新模块情况下添加字段,而且可能比雇佣某人编写自定义作业更便宜。
发布于 2013-07-16 14:38:14
如果您要将城市配置为下拉选项,请执行以下步骤:
步骤
假设已经创建了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;
}
}Notes:您还可以在数据库表中填充城市,以使其更具动态性。例如,您可以创建类似于regions的以下表:
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>的城市选项
以类似的方式,您可以将其他国家/地区的城市选项配置为well.In,也可以以类似的方式配置其他国家/地区的城市选项以及Cheers。
发布于 2013-10-02 01:46:49
如果您查看data文件夹中的Magento模块"Directory“:data/directory_setup/data-install-1.6.0.0.php文件,您可以看到Magento是如何填充表的。
例如,要添加澳大利亚的州,首先使用以下文件创建一个模块:
app/code/local/Package/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>然后你需要编写你的助手类: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'),
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'));
foreach ($data as $row) {
$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);}
这将添加州,当您在结帐页面上时,如果您选择国家澳大利亚,它将使用插入的州填充“州”下拉列表。
https://stackoverflow.com/questions/11195216
复制相似问题