2
votes

I have a magento store that needs to be able to ship orders to Curacao. By default this is grouped as Netherland Antilles in Magento, however, I found that even setting the order to this, wont get me the correct shipping rates from USPS even though international shipping is enabled.

I have added the country to magento via the en.xml file, and through the admin. I have enabled it as a country we can ship to under the USPS shipping method, and in the directory_country table in magento.

When the USPS API is called, I see this as a request and result:

2015-11-30T22:08:15+00:00 DEBUG (7): Array
(
[request] => <?xml version="1.0" encoding="UTF-8"?>
<IntlRateV2Request USERID="725CIGAR7315"><Revision>2</Revision><Package ID="0"><Pounds>0</Pounds><Ounces>0.8</Ounces><MailType>All</MailType><ValueOfContents>1</ValueOfContents><Country/><Container>VARIABLE</Container><Size>REGULAR</Size><Width/><Length/><Height/><Girth/></Package></IntlRateV2Request>

[result] => <?xml version="1.0" encoding="UTF-8"?>
<IntlRateV2Response><Package ID="0"><Error><Number>-2147219080</Number><Source>;IntlRateV2.UnpackIntlRateV2Node</Source><Description>Missing value for Country.</Description><HelpFile/><HelpContext/></Error></Package></IntlRateV2Response>
[__pid] => 2786
 )

Notice the empty Country tag in the request. I have dug through the code, and see where the country is supposed to be set via the app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php file.

Of course, I have to override this file (not edit core magento directly) which i have done, but still I cannot get the Country tag to populate on the send. I added this to the setRequest function:

    Mage::log("Destination Country: ");
    Mage::log($request->getDestCountryId());

    if ($request->getDestCountryId()) {
        $destCountry = $request->getDestCountryId();
    } else {
        $destCountry = self::USA_COUNTRY_ID;
    }

    $r->setDestCountryId($destCountry); 

This outputs in my log "CW" which is the correct country code for the transaction, based on my DB settings. So what gives? Why doesnt the country code get passed to the API so I can get some pricing?

I figure this isnt a new issue, and that Im probably looking over something painfully obvious, so I was hoping someone out there can point me in the right direction as I am currently "stumped".

Thanks in advance!

2

2 Answers

1
votes

The problem is that the USPS API takes full country names instead of ISO standard two letter country codes.

In this case your country code CW maps to "Curacao". These country names must be exact.

For a list of country code to USPS name mapping see:

https://docs.rocketship.it/php/1-0/usps-country-codes.html

For an official list of country names (no code mapping) see:

http://pe.usps.com/text/Imm/immctry.htm

0
votes

So after some fairly exhaustive code tracing, and research I have figured out how to get it all working. It is a multi step process but it let me get Curacao, Bonaire, and Montenegro working with USPS. Im sure this same fix will apply to other countries - I just havent discovered any others yet that reflect the same issue yet.

Step 1

Find out the ISO2 and ISO3 country codes for the countries you want to add or enable to your USPS. I found what I needed on wikipedia.

Step 2

Edit your stores language file(s) in my case I only had one, which is en.xml, (located in "/lib/Zend/Locale/Data/en.xml"). Find the territories xml block:

<territories>

     <territory type="xxxx">Country</territory>

And add your territories (if they dont already exist - in my case Montenegro already did exist here).

  <territory type="CW">Curacao</territory>
  <territory type="BQ">Bonaire</territory>

Save this file.

Step 3

Login to your mysql database and insert the following rows into the directory_country table.

 insert into directory_country values('CW','CW','CUW');
 insert into directory_country values('BQ','BQ','BES');

Step 4

Edit the Usps Shipping Carrier class located in the core Mage libs. DO NOT MODIFY THE ACTUAL CORE - INSTEAD CREATE A LOCAL MODIFIED VERSION IN app/code/local. I will assume you know how to do this already.

In the file "app/code/local/Mage/Usa/Model/Shipping/Carrier/Usps.php" find the function called:

protected function _getCountryName($countryId)

Add the new countries to the array like this:

'BQ' => 'Bonaire',
'CW' => 'Curacao',

Mapping your ISO2 code to the Official USPS country name.

Save this file.

Step 5

Login to your stores admin. Disable compile mode, refresh your caches, and indexes. Go to Configuration > Web > General and enable your new countries. Save this change.

Step 6

Go to Config > Sales > Shipping Methods. Click on USPS, and Enable the new countries. If you have all countries already selected, set the drop down to selected countries, and then back to All Countries. This will force the selection of the newly added countries. Save this change.

Step 7

Re-enable and force a recompile of compile mode (if your using it), and take the new countries for a spin!