title: Magento - One page checkout setting shipping country programmatically
body: <p>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'.</p>
  
  <p>I have followed the guide from: <a href="http://www.magentoworks.net/how-to-add-new-country-in-magento" rel="nofollow">http://www.magentoworks.net/how-to-add-new-country-in-magento</a>
  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. </p>
  
  <p>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. </p>
  
  <p>I am having problems trying to get this to work. From looking at shipping.phtml the country which is selected is called with: </p>
  
  <pre><code>   &lt;label for="shipping:country_id" class="required"&gt;&lt;em&gt;*&lt;/em&gt;&lt;?php echo $this-&gt;__('Country') ?&gt;&lt;/label&gt;
          &lt;div class="input-box"&gt;
              &lt;?php echo $this-&gt;getCountryHtmlSelect('shipping') ?&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>I have tracked down the function:</p>
  
  <pre><code>public function getCountryHtmlSelect($type)
  </code></pre>
  
  <p>Which is in: </p>
  
  <pre><code>Mage/Checkout/Block/Onepage/Abstract.php
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>public function getCountryHtmlSelect($type)
  {
      $countryId = $this-&gt;getAddress()-&gt;getCountryId();
  
      if($countryId == 'ZD' || $countryId == 'ZB' || $countryId == 'ZC'):
  
          $newCountryId = "GB";
  
      else:
  
          $newCountryId = $countryId;
  
      endif;
  
      if (is_null($countryId)) {
          $countryId = Mage::helper('core')-&gt;getDefaultCountry();
      }
      $select = $this-&gt;getLayout()-&gt;createBlock('core/html_select')
          -&gt;setName($type.'[country_id]')
          -&gt;setId($type.':country_id')
          -&gt;setTitle(Mage::helper('checkout')-&gt;__('Country'))
          -&gt;setClass('validate-select')
          -&gt;setValue($newCountryId)
          -&gt;setOptions($this-&gt;getCountryOptions());
      if ($type === 'shipping') {
          $select-&gt;setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
      }
  
      return $select-&gt;getHtml();
  }
  </code></pre>
  
  <p>However, when I go through the checkout and to the payment gateway I get a failure (because its still sending the custom country code.</p>
  
  <p>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? </p>
  
  <p>Any help is much appreciated. Thank you. </p>