【问题标题】:Magento - One page checkout setting shipping country programmaticallyMagento - 以编程方式设置发货国家/地区的一页结帐
【发布时间】:2014-03-29 17:18:53
【问题描述】:

我有一个客户的请求,他们想在 Magento 中添加额外的国家。 例如,除了“United Kingdom”,我们还添加了“Wales”、“Scotland”、“England”。

我遵循了来自:http://www.magentoworks.net/how-to-add-new-country-in-magento 的指南 并在管理面板中设置新的国家发货。 这很好并且有效,但是指南提到需要在数据库中使用唯一的国家代码。

出现的问题是该网站使用的支付网关只接受默认的 Magento 网关,并且由于向网站添加国家/地区是装饰性的,我认为最好的想法是在结帐时,一旦收货地址选择然后我可以检查任何新的国家代码,然后将它们设置为“英国”国家代码。

我在尝试使其正常工作时遇到问题。通过查看 shipping.phtml,选择的国家被称为:

   <label for="shipping:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
        <div class="input-box">
            <?php echo $this->getCountryHtmlSelect('shipping') ?>
        </div>

我已经追踪到了函数:

public function getCountryHtmlSelect($type)

其中:

Mage/Checkout/Block/Onepage/Abstract.php

从这里我尝试覆盖和自定义函数,以便如果国家代码是自定义代码之一,那么它将设置为我希望可以工作的“GB”:

public function getCountryHtmlSelect($type)
{
    $countryId = $this->getAddress()->getCountryId();

    if($countryId == 'ZD' || $countryId == 'ZB' || $countryId == 'ZC'):

        $newCountryId = "GB";

    else:

        $newCountryId = $countryId;

    endif;

    if (is_null($countryId)) {
        $countryId = Mage::helper('core')->getDefaultCountry();
    }
    $select = $this->getLayout()->createBlock('core/html_select')
        ->setName($type.'[country_id]')
        ->setId($type.':country_id')
        ->setTitle(Mage::helper('checkout')->__('Country'))
        ->setClass('validate-select')
        ->setValue($newCountryId)
        ->setOptions($this->getCountryOptions());
    if ($type === 'shipping') {
        $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
    }

    return $select->getHtml();
}

但是,当我结帐并到达支付网关时,我遇到了故障(因为它仍在发送自定义国家代码。

当用户刚刚完成结帐的“送货”步骤时,在单页结帐中更改送货国家代码的最佳方法是什么?

非常感谢任何帮助。谢谢你。

【问题讨论】:

  • getCountryHtmlSelect 以前端形式打印国家/地区列表。

标签: magento magento-1.7 checkout


【解决方案1】:

试试这个:

  1. 在结帐时,设置隐藏字段 `
  2. 创建另一个字段来选择国家/地区列表(或者只是将当前选择器重命名为 name="billing[country_id2]"

您将始终获得 GB 作为国家/地区。

要执行 #2,您必须覆盖 Mage_Checkout_Block_Onepage_Shipping 并添加以下函数。

public function getCountryHtmlSelect($type)
{
    $countryId = $this->getAddress()->getCountryId();
    if (is_null($countryId)) {
        $countryId = Mage::helper('core')->getDefaultCountry();
    }
    $select = $this->getLayout()->createBlock('core/html_select')
        ->setName($type.'[country_id2]')
        ->setId($type.':country_id')
        ->setTitle(Mage::helper('checkout')->__('Country'))
        ->setClass('validate-select')
        ->setValue($countryId)
        ->setOptions($this->getCountryOptions());
    if ($type === 'shipping') {
        $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
    }

    return $select->getHtml();
}

注意更改-&gt;setName($type.'[country_id2]') 行。仅供参考,Mage_Checkout_Block_Onepage_Abstract 中定义的上述函数,我们只是在这里覆盖以进行运输。

您还需要在客户地址上添加附加属性以保存 country_id2 值。你可以很容易地像这样http://www.magentocommerce.com/magento-connect/customer-attributes-manager-1.html

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2014-10-25
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多