3

How can I enabled USPS Shipping method for APO Addresses Only? I have USPS working fine, however we only want to use it for US military addresses (APO, FPO or DPO) only. Our primary shipping method is UPS which is working fine, however our customers are now selecting USPS address and we are loosing money on those shipping methods.

How can I lock down USPS shipping method to only be an option for APO addresses? Currently in Magento backend there is only options to select Applicable Countries.

Thanks

  • APO/FPO/DPO addresses fall into well defined and permanent Zipcode blocks for AA, AE, AP which can be used to determine the freight method. – Fiasco Labs Dec 23 '13 at 20:31
  • SCF ranges for AE 090-098 || AA 340 || AP 962-966 – Fiasco Labs Dec 23 '13 at 20:41
  • 1
    @FiascoLabs: I can't find a way to setup shipping methods to allow USPS for only zipcodes; can you post more information? – Navarr Oct 29 '14 at 14:33
  • Magento admin does not support setting up USPS to allow only specific zip codes. It only supports certain countries. – CarComp Jan 28 '15 at 14:17
2

I used the following code in my ../template/checkout/onepage/shipping_method/available.phtml custom theme template inside the foreach shippingrategroups loop....

USPS and additional shipping methods must be enabled. It forces any APO address to USPS if it finds a city or region code that matches.

         <?php //Added to force USPS for APO
            $address = $this->getAddress();
            $apoStateCodes = array("AE", "AE", "AP");
            $apoCityCodes = array("APO", "FPO", "DPO");

            $regionCode = Mage::getModel('directory/region')->load($address->getRegionId())->getCode();

            $carrierName = strtoupper(str_replace(" ", "", $this->getCarrierName($code)));
            if (in_array($regionCode, $apoStateCodes) || in_array($address->getCity(), $apoCityCodes))
            {
                // IT IS APO, so skip iteration of non-USPS
                if ($carrierName != "USPS")
                {
                    continue;
                }
            }
            else
            {
                // ITS A NORMAL ADDRESS
                if ($carrierName == "USPS")
                {
                    continue;
                }
            }

            /* OLD WAY OF DOING THINGS (only works on PO box)
            if (preg_match("/p\.* *o\.* *box/i", $this->htmlEscape($this->getAddress()->getStreet(1))))
            {
                $carrierName = strtoupper(str_replace(" ", "", $this->getCarrierName($code)));
                if ($carrierName != "UNITEDSTATESPOSTALSERVICE")
                {
                    continue;
                }
            }
            */
         ?>
1

Use the function proccessAdditionalValidation within your Shipping Carrier Model

This function accepts Mage_Shipping_Model_Rate_Request $request should return false if the method should not be allowed.

You can get the postal code from the request

$request->getDestRegionCode()

I am not sure of the best way to determine if your postal code is acceptable or not short of building an array of them.

For example, here is how I limited USPS to PO Boxes, AK, and HI in a recent project

    public function proccessAdditionalValidation(Mage_Shipping_Model_Rate_Request $request)
{   
    $isPoBox =  preg_match("/p\.* *o\.* *box/i", $request->getDestStreet());

    //PO Box, AK, HI need to be forced to USPS shipping
    if ( ! $isPoBox && $request->getDestCountryId() == 'US' && ! in_array($request->getDestRegionCode(), array('AK', 'HI')) ) {
        return false;
    }                              
    return $this;
}  
  • PO Box is not the same as an APO/FPO/DPO address. Zipcode blocks for AA, AE, AP would be a lot better method. The ranges are permanent, without need to parse address lines to look for PSC 802 BOX 74 APO AE 09499-0074 || UNIT 2050 BOX 4190 APO AP 96278-2050 || USCGC HAMILTON FPO AP 96667-3931 – Fiasco Labs Dec 23 '13 at 20:29
  • SCF ranges for AE 090-098 || AA 340 || AP 962-966 – Fiasco Labs Dec 23 '13 at 20:42
  • I know PO is different, that is why I said For Example. It was not meant to be copy and paste code. – Ben Truby Jan 2 '14 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.