Disclaimer: I am not responsible for any failed or lost orders due to the implementation of this modification. It is up to you to fully test it on a staging system before deploying it to production.
This tends to be a relatively annoying issue for a lot of people. A customer order's and item (usually a rather large item) and has it shipped to a P.O. Box only to have UPS or FedEx reject the delivery. While it is true they can ship to Post Office Boxes not all of them are either accessible to UPS/FedEx or they are too small for the parcel in question. Unfortunately X-Cart by default gives no option for people to disable shipping via these methods to those addresses.
We've been using this modification for the past 2 years (though with slightly different logic) so I've decided to strip out the code sealed under NDA and share the open version with everyone else.
Note: The following modification was written around X-Cart versions 4.1 and 4.2 and has not been tested for 4.3.
The first file you need to modify is
shipping/myshipper.php. Open the file and at the very bottom of the code add the following function:
PHP Code:
// {{{ func_is_postal_box()
/**
* This function checks to see if the customer's
* shipping address is a United States Post Office
* Box.
*
* @param string $address Customer's Street Address
* @return boolean true if post office box; false if not
*/
function func_is_postal_box($address) {
$postalBoxRegex = '/^([pP]{1}(.*?)[oO]{1}(.*?))?([bB][oO]?[xX])(\s+)([0-9]+)/iD';
@preg_match($postalBoxRegex, trim($address), $isPostalBox);
if (is_array($isPostalBox) && (count($isPostalBox) > 0)) {
return true;
}
else {
return false;
}
}
// }}}
Next you will need to edit two files,
shipping/mod_UPS.php and depending if you're using X-Cart 4.1 or 4.2 either
shipping/mod_FEDEX.php (4.2) or
shipping/mod_FEDEX_direct.php (4.1)
shipping/mod_UPS.php:
After:
Add:
PHP Code:
/* Check for the existance of a P.O. Box Address */
if (func_is_postal_box($userinfo['s_address'])) return;
shipping/mod_FEDEX.php (4.2) or
shipping/mod_FEDEX_direct.php (4.1)
After:
PHP Code:
if (!$FEDEX_FOUND)
return;
Add:
PHP Code:
/* Check for the existance of a P.O. Box Address */
if (func_is_postal_box($userinfo['s_address'])) return;
Once you have made these modifications truncate the xcart_shipping_cache table and you should be good to go. This will disable UPS and FedEx for any address that is considered a P.O. Box by the regular expression (this expression has been thoroughly tested against thousands of customers who tried to get around it)
Enjoy