Switch UPS Account Based on Shipping Class with UPS WooCommerce Shipping.

One of our customers, Scott, presented an interesting business case. He ships from two warehouses, located quite far from each other (one located on the East Coast of the US and the other in the West.) He maintains separate UPS accounts for each warehouse and wanted to use them selectively depending on which warehouse was closest to the customer.

Scott asked us if it is possible to connect more than one UPS accounts and choose which one to use for a particular order. In response, we devised a workaround for this case as described below:

When products belonging to shipping class ‘X’ are in the order, the plugin will select UPS Account number ‘A’ for label printing. When the products belonging to shipping class ‘Y’  are in the order, the plugin will select UPS Account number ‘B’. As you might be wondering, when products from both shipping classes (X and Y) are in the cart, the plugin will select UPS Account number ‘A’ again.

We developed a code snippet that enables to switch UPS accounts based on shipping class when using our UPS WooCommerce Shipping Plugin.

After updating your alternate UPS account number into the following code snippet, you can simply add the code snippet to your functions.php:

add_filter('wf_ups_shipment_settings', 'wf_ups_modify_settings_datas', 10, 3);
function wf_ups_modify_settings_datas($settings, $order){
// Add alternative accounts
$alternate_accounts = array(
'class-a' => array( // shipping class to use alternative account
//Alternative account details.
'shipper_number' => 'XXXXXXXXXX', // UPS account number
'user_id' => 'XXXXXXXXXX', // UPS user ID
'password' => 'XXXXXXXXXX', // UPS password
'access_key' => 'XXXXXXXXXX', // UPS access key
'origin_addressline'=> '420 9th Ave', // Origin address
'origin_postcode' => '10001', // Origin postcode
'origin_city' => 'New York', // Origin City
'origin_state' => 'NY', // Origin State
'origin_country' => 'US', // Origin Country
),
'shipping-class-b' => array( // shipping class to use alternative account
'shipper_number' => '1111', // UPS account number
'user_id' => '111', // UPS user ID
'password' => '111111', // UPS password
'access_key' => '111111111', // UPS access key
'origin_addressline'=> '200 exp Ave', // Origin address
'origin_postcode' => '30303', // Origin postcode
'origin_city' => 'Georgia', // Origin City
'origin_state' => 'GY', // Origin State
'origin_country' => 'US', // Origin Country
)
);

global $woocommerce;
if( !is_cart() && empty($order) ){
return $settings;
}

foreach ($alternate_accounts as $shipping_class => $alternate_account) {
$is_all_items_belog_shipping_class = xa_is_all_items_belog_shipping_class($shipping_class, $order);
if( $is_all_items_belog_shipping_class == true ){
break;
}
}
if( $is_all_items_belog_shipping_class == false ){
return $settings;
}

$settings['shipper_number'] = $alternate_accounts[$shipping_class]['shipper_number'];
$settings['user_id'] = $alternate_accounts[$shipping_class]['user_id'];
$settings['password'] = $alternate_accounts[$shipping_class]['password'];
$settings['access_key'] = $alternate_accounts[$shipping_class]['access_key'];
$settings['origin_addressline'] = $alternate_accounts[$shipping_class]['origin_addressline'];
$settings['origin_postcode'] = $alternate_accounts[$shipping_class]['origin_postcode'];
$settings['origin_city'] = $alternate_accounts[$shipping_class]['origin_city'];
$settings['origin_state'] = $alternate_accounts[$shipping_class]['origin_state'];
$settings['origin_country'] = $alternate_accounts[$shipping_class]['origin_country'];
return $settings;
}

function xa_is_all_items_belog_shipping_class( $shipping_class, $order='' ){
if( !is_array( $shipping_class ) ){
$shipping_class = array( $shipping_class );
}
//For Label side
if( !empty($order) ){
foreach ($order->get_items() as $key => $item) {
$product = $order->get_product_from_item($item);
if ( !in_array( $product->get_shipping_class(), $shipping_class ) ) {
return false;
}
}
}else{ //For Rate
global $woocommerce;
foreach(WC()->cart->cart_contents as $key => $values) {
if(!in_array($values['data']->get_shipping_class() , $shipping_class)) {
return false;
}
}
}
return true;
}

In the above code, there is an array $alternate_account where you  can configure the UPS account details (B) and a shipping class (Y) in the array.

If a particular order has only products belonging to this shipping class (Y), the alternative account (B) will be used, otherwise use default account (A).

Last Updated On February 19, 2019

4 thoughts on “Switch UPS Account Based on Shipping Class with UPS WooCommerce Shipping.

  1. nickh says:

    Does this snippet for your UPS basic or just the premium? I have it place with the basic and it is not functioning.

    • Anindo
      Anindo C says:

      Hi Nick,

      This snippet works with the premium plugin only. Unfortunately, we do not have a similar compatible snippet for the basic version. You can use our premium version to work with this functionality.

  2. Destiny
    Destiny says:

    My client ships wine and merchandise, which must be packaged and shipped separately, under different UPS account numbers. This snippet is almost what we need.

    “As you might be wondering, when products from both shipping classes (X and Y) are in the cart, the plugin will select UPS Account number ‘A’ again.” Can you help me change this so that both UPS Account numbers will be used, instead of defaulting to just one?

    Thanks so much!

    Diane, Destiny’s developer

    • Anindo
      Anindo C says:

      Hi Diana,

      We are already in communication with you on this . Our team will get back to you via the ticket as soon as possible.

      Let us know if you have any query.

Comments are closed.