X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Disable UPS and FedEx for P.O. Boxes (https://forum.x-cart.com/showthread.php?t=51646)

exsecror 01-07-2010 08:32 AM

Disable UPS and FedEx for P.O. Boxes
 
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($postalBoxRegextrim($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:
PHP Code:

if (!$UPS_FOUND)
    return; 


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

cflsystems 01-07-2010 12:14 PM

Re: Disable UPS and FedEx for P.O. Boxes
 
This should really be in the core code of xcart. Thanks Alan

exsecror 01-08-2010 03:51 AM

Re: Disable UPS and FedEx for P.O. Boxes
 
Quote:

Originally Posted by cflsystems
This should really be in the core code of xcart. Thanks Alan


I agree, I also have similar procedures for blocking real-time shipping methods to zones (which it cannot do by default) and I also patched them to pass the customer's shipping address in the lookup for accurate rates (something else it should be doing by default but isn't)

DocOrtho 03-11-2010 01:37 PM

Re: Disable UPS and FedEx for P.O. Boxes
 
I have flat rates and don't use real time calculations. It was just as easy to edit the shipping/shipping.php file:

Around line 288
Code:

  if  (!$enable_shipping ||  $config["Shipping"]["realtime_shipping"] !=  "Y") {
      #
      # Get ALL shipping methods  according to the conditions (W/O real time)
      #
      $shipping = func_query("SELECT  * FROM $sql_tbl[shipping] WHERE active='Y' $destination_condition  $weight_condition ORDER BY orderby");

Change to:
Code:

if (!$enable_shipping || $config["Shipping"]["realtime_shipping"] != "Y") {
  #
  # Get ALL shipping methods according to the conditions (W/O real time)
  #
  // {{{ 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;
  }
}
// }}} 
  if (func_is_postal_box($userinfo['s_address'])){
  $shipping = func_query("SELECT * FROM $sql_tbl[shipping] WHERE active='Y' AND code='USPS' $destination_condition $weight_condition ORDER BY orderby");
  }else{
  $shipping = func_query("SELECT * FROM $sql_tbl[shipping] WHERE active='Y' $destination_condition $weight_condition ORDER BY orderby");
  }


Kiss 03-10-2011 02:25 PM

Re: Disable UPS and FedEx for P.O. Boxes
 
I implemented this code and it works properly. The only issue I have since I am using one page checkout in 4.4 2, I have to refresh a screen in order to display if shipping is allowed. Any advise? Also how can I display a message indicatting that we do not ship to P.O. Boxes ?

Thanks,
Fred.


All times are GMT -8. The time now is 03:57 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.