0
votes

I have a request from a client where they want to add extra countries in Magento. For example along with 'United Kingdom' we added, 'Wales', 'Scotland', 'England'.

I have followed the guide from: http://www.magentoworks.net/how-to-add-new-country-in-magento and set the new countries in the admin panel for shipping. This is fine and works, however the guide mentioned that unique country codes needed to be used in the database.

The problem which arises is that the payment gateway which the site uses only accepts the default Magento ones and as this addition of countries to the site is cosmetic, I thought the best idea would be that in the checkout, once the shipping address is chosen then I can check for any of the new country codes, then set them to the 'United Kingdom' country code.

I am having problems trying to get this to work. From looking at shipping.phtml the country which is selected is called with:

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

I have tracked down the function:

public function getCountryHtmlSelect($type)

Which is in:

Mage/Checkout/Block/Onepage/Abstract.php

From here I tried to override and customize the function so that if the country code was one of the custom ones then it would set to 'GB' which I hoped would work:

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();
}

However, when I go through the checkout and to the payment gateway I get a failure (because its still sending the custom country code.

What is the best way to change the shipping country code in the one page checkout when the user has just completed the 'Shipping' step of the checkout?

Any help is much appreciated. Thank you.

1
getCountryHtmlSelect is to print list of country in front-end form. - Krishna Sunuwar

1 Answers

1
votes

Try this:

  1. In your checkout, make hidden field `
  2. Make another field to select country list (or just rename current current selector to name="billing[country_id2]"

You will always get GB as country.

To do #2, you have to override Mage_Checkout_Block_Onepage_Shipping and add following function.

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();
}

Note changes line ->setName($type.'[country_id2]'). FYI, above function defined in Mage_Checkout_Block_Onepage_Abstract, we are just overriding here for shipping.

You also need add additional attribute on customer address to save country_id2 value. You easily can do this extensions like this http://www.magentocommerce.com/magento-connect/customer-attributes-manager-1.html