Solving USPS shipping rate change issue with magento

On 2nd January 2011 USPS updated their shipping rates and made some changes to the data types and grammar of their third party connection module also they change method name. Many of the magento stores around the web stopped displaying the shipping rates from USPS. Here I am putting a guide to solve this issue and get the shipping rates displayed on magento store front.

I have tested this with Magento 1.3.0.0 and work with all other with relevant changes. Take care about the line numbers as those may change with respect to editor you use.

Step 1:

File location: /app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php

On line 279 you will find following code

(“foreach ($xml->Package->Postage as $postage) {”,

Add this code after above line:


$postage->MailService = str_replace("<sup>®</sup>","",(string)$postage->MailService);

Step 2:

The fix for international is very similar to the original fix. Below the current fix about 20 lines you will find following code “foreach ($xml->Package->Service as $service) {“

Add this code after above line:


$service->SvcDescription = str_replace("<sup>®</sup& gt;","",(string)$service->SvcDescription);

Step: 3

Fix for First-class mail and international

Go to In file: /app/code/core/Mage/Usa/etc/config.xml

Find similar this:


<allowed_methods>Bound Printed Matter,Express Mail,Express Mail Flat-Rate Envelope,Express Mail Flat-Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International,Express Mail International (EMS) Flat-Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed,Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Flat-Rate Box,Priority Mail Flat-Rate Envelope,Priority Mail International,Priority Mail International Flat-Rate Box,Priority Mail International Flat-Rate Envelope,Priority Mail International Large Flat-Rate Box,Priority Mail Large Flat-Rate Box,USPS GXG Envelopes</allowed_methods>

And Replace with this code


<allowed_methods>Bound Printed Matter,Express Mail,Express Mail Flat-Rate Envelope,Express Mail Flat-Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International (EMS),Express Mail International (EMS) Flat-Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed,Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Flat-Rate Box,Priority Mail Flat-Rate Envelope,Priority Mail International,Priority Mail International Flat-Rate Box,Priority Mail International Flat-Rate Envelope,Priority Mail International Large Flat-Rate Box,Priority Mail Large Flat-Rate Box,USPS GXG Envelopes,First-Class Mail Package,First-Class Mail International Package**</allowed_methods>

Step: 4

Then Find similar this:


<methods>Bound Printed Matter,Express Mail,Express Mail Flat-Rate Envelope,Express Mail Flat-Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International (EMS),Express Mail International (EMS) Flat-Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed,Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Flat-Rate Box,Priority Mail Flat-Rate Envelope,Priority Mail International,Priority Mail International Flat-Rate Box,Priority Mail International Flat-Rate Envelope,Priority Mail International Large Flat-Rate Box,Priority Mail Large Flat-Rate Box,USPS GXG Envelopes</methods>

And Replace with


<methods>Bound Printed Matter,Express Mail,Express Mail Flat-Rate Envelope,Express Mail Flat-Rate Envelope Hold For Pickup,Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee,Express Mail Hold For Pickup,Express Mail International (EMS),Express Mail International (EMS) Flat-Rate Envelope,Express Mail PO to PO,Express Mail Sunday/Holiday Guarantee,First-Class Mail International Large Envelope,First-Class Mail International Letters,First-Class Mail International Package,First-Class,First-Class Mail,First-Class Mail Flat,First-Class Mail International,First-Class Mail Letter,First-Class Mail Parcel,Global Express Guaranteed,Global Express Guaranteed Non-Document Non-Rectangular,Global Express Guaranteed Non-Document Rectangular,Library Mail,Media Mail,Parcel Post,Priority Mail,Priority Mail Flat-Rate Box,Priority Mail Flat-Rate Envelope,Priority Mail International,Priority Mail International Flat-Rate Box,Priority Mail International Flat-Rate Envelope,Priority Mail International Large Flat-Rate Box,Priority Mail Large Flat-Rate Box,USPS GXG Envelopes,First-Class Mail Package,First-Class Mail International Package**</methods>

Step: 5

Go to In file /app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php

Find function getCode($type, $code='') and replace with this code

public function getCode($type, $code='')

{

$codes = array(

'service'=>array(

'FIRST CLASS' => Mage::helper('usa')->__('First-Class'),

'First-Class Mail International Package**' => Mage::helper('usa')->__('First-Class Mail International Package**'),

'PRIORITY'    => Mage::helper('usa')->__('Priority Mail'),

'EXPRESS'     => Mage::helper('usa')->__('Express Mail'),

'BPM'         => Mage::helper('usa')->__('Bound Printed Matter'),

'PARCEL'      => Mage::helper('usa')->__('Parcel Post'),

'MEDIA'       => Mage::helper('usa')->__('Media Mail'),

'LIBRARY'     => Mage::helper('usa')->__('Library'),

//                'ALL'         => Mage::helper('usa')->__('All Services'),

),

'method'=>array(

'First-Class',

'First-Class Mail International Package',

'Express Mail',

'Express Mail PO to PO',

'Priority Mail',

'Parcel Post',

'Express Mail Flat-Rate Envelope',

'Priority Mail Flat-Rate Box',

'Bound Printed Matter',

'Media Mail',

'Library Mail',

'Priority Mail Flat-Rate Envelope',

'Global Express Guaranteed',

'Global Express Guaranteed Non-Document Rectangular',

'Global Express Guaranteed Non-Document Non-Rectangular',

'Express Mail International (EMS)',

'Express Mail International (EMS) Flat Rate Envelope',

'Priority Mail International',

'Priority Mail International Flat Rate Box',

),

'service_to_code'=>array(

'First-Class'                      => 'FIRST CLASS',

'First-Class Mail International Package**'          => 'First-Class Mail International Package**',

'Express Mail'                     => 'EXPRESS',

'Express Mail PO to PO'            => 'EXPRESS',

'Priority Mail'                    => 'PRIORITY',

'Parcel Post'                      => 'PARCEL',

'Express Mail Flat-Rate Envelope'  => 'EXPRESS',

'Priority Mail Flat-Rate Box'      => 'PRIORITY',

'Bound Printed Matter'             => 'BPM',

'Media Mail'                       => 'MEDIA',

'Library Mail'                     => 'LIBRARY',

'Priority Mail Flat-Rate Envelope' => 'PRIORITY',

'Global Express Guaranteed'        => 'EXPRESS',

'Global Express Guaranteed Non-Document Rectangular'     => 'EXPRESS',

'Global Express Guaranteed Non-Document Non-Rectangular' => 'EXPRESS',

'Express Mail International (EMS)'                       => 'EXPRESS',

'Express Mail International (EMS) Flat-Rate Envelope'    => 'EXPRESS',

'Priority Mail International'                            => 'PRIORITY',

'Priority Mail International Flat-Rate Box'              => 'PRIORITY',

'Priority Mail International Large Flat-Rate Box'        => 'PRIORITY'

),

'first_class_mail_type'=>array(

'LETTER'      => Mage::helper('usa')->__('Letter'),

'FLAT'        => Mage::helper('usa')->__('Flat'),

'PARCEL'      => Mage::helper('usa')->__('Parcel'),

),

'container'=>array(

'VARIABLE'           => Mage::helper('usa')->__('Variable'),

'FLAT RATE BOX'      => Mage::helper('usa')->__('Flat Rate Box'),

'FLAT RATE ENVELOPE' => Mage::helper('usa')->__('Flat Rate Envelope'),

'RECTANGULAR'        => Mage::helper('usa')->__('Rectangular'),

'NONRECTANGULAR'     => Mage::helper('usa')->__('Non-rectangular'),

),

'size'=>array(

'REGULAR'     => Mage::helper('usa')->__('Regular'),

'LARGE'       => Mage::helper('usa')->__('Large'),

'OVERSIZE'    => Mage::helper('usa')->__('Oversize'),

),

'machinable'=>array(

'true'        => Mage::helper('usa')->__('Yes'),

'false'       => Mage::helper('usa')->__('No'),

),

);

$methods = $this->getConfigData('methods');

if (!empty($methods)) {

$codes['method'] = explode(",", $methods);

} else {

$codes['method'] = array();

}

if (!isset($codes[$type])) {

//            throw Mage::exception('Mage_Shipping', Mage::helper('usa')->__('Invalid USPS XML code type: %s', $type));

return false;

} elseif (''===$code) {

return $codes[$type];

}

if (!isset($codes[$type][$code])) {

//            throw Mage::exception('Mage_Shipping', Mage::helper('usa')->__('Invalid USPS XML code for type %s: %s', $type, $code));

return false;

} else {

return $codes[$type][$code];

}

}

Then refresh cache

Then go to shipping method in admin > under USPS > Allowed method > select First-Class Mail International Package** and First-Class Mail Package.

Refresh cache.

To prevent you from changing core code and can be removed once Magento has posted a fix. Move your code in app/local/ folder

example

File to be modified location: /app/code/core/Mage/Usa/Model/Shipping/Carrier/Usps.php

Move to new location: /app/code/local/Mage/Usa/Model/Shipping/Carrier/Usps.php

And

/app/code/core/Mage/Usa/etc/config.xml

Move to new location:

/app/code/local/Mage/Usa/etc/config.xml

Hope this help 🙂

Advertisements

35 Responses to “Solving USPS shipping rate change issue with magento”

  1. Jesse Says:

    We are working on tiecoon.com and I think you are a genius…everybody else hase the most confusing threads. Will keep you posted to the success.

  2. Carl Says:

    Thanks for posting this fix!

    The extra spaces in the code in line 2 had me messed up for a while, though.

  3. Quentin Says:

    I’m still receiving the shipping unavailable error.
    I’m using magento 1.4.2 beta 1
    Any advice?

  4. mospor Says:

    I did all the above I replaced step 5 with the old one but now when i go to the admin the page will not load.

  5. Raul Zendejas Says:

    Thank you so much for the clear info! I am on magento 1.4.1.1 and ran into a small issue for international. I had to remove the spaces from the code you have above.
    So it went from this

    $service->SvcDescription = str_replace(“& lt;sup& gt;& amp;reg;& lt;/sup& gt;”,””,(string)$service->SvcDescription);

    To this

    $service->SvcDescription = str_replace(“<sup>&reg;</sup>”,””,(string)$service->SvcDescription);

    Once again Thanks! Hope this helps someone .

  6. ride-this.com Says:

    I’m having the same problem as mospor. I’m on 1.3.2.4 and when I implement these fixes, System >> Configuration >> Sales >> Shipping Methods just turns to a white screen.

    I’ve tried cache refresh, I do not use compiling.

    Replacing the edited file with my original brings that admin page back, but the USPS error is obviously still there.

    Any suggestions, Indies?

  7. Quentin Says:

    Thanks for the excellent post, a good summary of all that was mentioned on the Megento forum.

    I have only one comment, and it’s more of a best practice suggestion. Instead of editing Magento core files, create the same file structure for your edited files in /app/local instead.

    Therefore, your new files will be located in /app/code/local/Mage/Usa/etc/config.xml and /app/code/core/Mage/Usa/Model/Shipping/Carier/Usps.php instead.

    These files will override your core files and is recommended instead of editing core files.

  8. Ted Says:

    Great article. Can you clarify what you mean in step 5? Thanks.

    • Team Indies Says:

      on step 5 Find function getCode($type, $code=”) and replace with given code
      then refresh cache
      Then go to shipping method in admin > under USPS > Allowed method > select First-Class Mail International Package** and First-Class Mail Package.
      Refresh cache.

  9. Bill Says:

    Thanks so much for the post! It got things working for us (until the next change in May 😉

    BTW, there’s an extra space in the code for Step 2 – caused a bit of pondering on my end until I spotted the problem.

    /supAMP gt;”,”” should be /supAMPgt;”,””

  10. Sherry Says:

    It worked!! Holy…mother of…I am relieved! Well, the only part that didn’t work was that the config file refused to be found when I placed a copy in local. So I renamed the one in core and replaced with the updated one. Works like a charm! Will deal with the core vs. local issue later…unless someone can tell me why the local version was being ignored.

    Thanks a TON!

  11. Dennis Says:

    can someone post their entire USPS.php file?? I’m having difficulty on 1.3.3.0

  12. John Says:

    This worked perfectly. I just wish I had found it sooner. Thanks and Cheers!

  13. Lindsay Says:

    Has this worked for people that use one-step-checkout??

  14. Jen Says:

    Can i just say something? everyone says to make sure you put files in /local instead of /core, yet i tried for two hours to get this to work, and it never worked, until i tried overwriting the /core. Putting it in /local did not make this work at all. It’s been so frustrating!

    • Team Indies Says:

      create a Mage folder in your local folder and put file as per your url in our case
      usps file path is
      app/code/local/Mage/Usa/Model/Shipping/Carrier/Usps.php
      app/code/local/Mage/Usa/etc/config.xml
      app/code/local/Mage/Usa/etc/system.xml

      Thanks

  15. PAEX Says:

    We are unable to get anything to work. We are on Magento CE 1.4.0.1. I really need a way to get a debug log of the shipping issues. Can someone explain how to enable a detailed log of the problem? I have already enabled logging but I do not see any specific logging options for USPS.
    Thank you

  16. Irene Says:

    Life saver~ Thank you so much! It works perfect.

  17. Zang Says:

    Can you email me the two php and xml files that you used for this? It’s easier if I just use FTP to transfer the files. No matter what I do, it’s not working. I must not be copying and pasting right or what not?? Been trying to work on this for weeks and I can’t even post on Magento either because the darn captcha is not working right! So frustrated. Please help.

  18. anna Mariyah Says:

    Team Indies, U gotta help me. I am willing to pay to make my usps work. I tried everything and I did everything u said. It doesnt work. Can u please email me so I can talk further with u?
    Thank you. Anna

  19. Rob Says:

    Great post and looked promising at first, expect I wasn;t able to locate the first line of coe mentioned in step 1.

    I’m using Magento ver. 1.3.2.4… any ideas?!

    I been struggling with this USPS calculator for months now and need to get this sorted out. I am happy to pay a fee.

    Any help is much appreciated.
    Thanks, Rob

    • Rob Says:

      OK, after some further digging I got somewhere, except when testing the calculator I get this: Parse error: syntax error, unexpected ‘)’, expecting T_STRING or T_VARIABLE or ‘{‘ or ‘$’ in /home/rob/public_html/bhaktistore.com/app/code/local/Mage/Usa/Model/Shipping/Carrier/Usps.php on line 273

      Help!

  20. Rob Says:

    OK, apologies for the ignorance in my end, but it now appears as if it works with your solution and a little digging myself….

    Your solution is THE only one that worked, after searching the Magento forum far and wide. Thanks guys.

    ciao
    Rob
    AMSTERDAM

  21. 1and1 internet login Says:

    Hello, I’ve a simple query. Does anybody get a slight lag using this site whenever they arrive? It needs at the very least 5 seconds or so for me to view the home web page. Many thanks

  22. buy local seo leads Says:

    I love it when people get together and share thoughts.
    Great site, continue the good work!


Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: