-
-
Save boywondercreative/a2590977b6dcfcb121c7aa95415ce72c to your computer and use it in GitHub Desktop.
Switch UPS account and origin address based on destination. wtih XAdapter UPS plugin WooCommerce UPS Shipping Plugin with Print Label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Snippet to Change UPS account based on destination country while confirming shipment (not while generating packages). | |
* | |
*/ | |
add_filter( 'wf_ups_shipment_confirm_request_data', 'switch_origin_adress_based_on_country', 10, 2 ); | |
if( ! function_exists('switch_origin_adress_based_on_country') ) | |
{ | |
function switch_origin_adress_based_on_country( $request, $order ) { | |
$ship_from_address = array( | |
'US' => array( // US address | |
'Name' => 'Varun', | |
'AttentionName' => 'Varun', | |
'PhoneNumber' => '1234567890', | |
'EMailAddress' => '[email protected]', | |
'ShipperNumber' => '589Y4Y', //UPS account Number | |
'Address' => array( | |
'AddressLine1' => '128th main', | |
'City' => 'california', | |
'StateProvinceCode' => 'CA', | |
'CountryCode' => 'US', | |
'PostalCode' => '90001', | |
), | |
'units' => 'imperial', //imperial for LBS /IN and metric for KG / CM | |
), | |
'EU' => array( // Europe address | |
'Name' => 'Varun2', | |
'AttentionName' => 'Varun2', | |
'PhoneNumber' => '0123456789', | |
'EMailAddress' => '[email protected]', | |
'ShipperNumber' => '589Y4Y', //UPS account Number | |
'Address' => array( | |
'AddressLine1' => '128th main', | |
'City' => 'california', | |
'StateProvinceCode' => 'CA', | |
'CountryCode' => 'FR', | |
'PostalCode' => '90001', | |
), | |
'units' => 'metric', | |
) | |
); | |
if( ! empty($ship_from_address['EU']) && in_array( strtoupper($request['Shipment']['ShipTo']['Address']['CountryCode']), WC()->countries->get_european_union_countries() ) ) { | |
$units = $shipper_address['units']; | |
unset( $shipper_address['units'] ); | |
$request['Shipment']['Shipper'] = $ship_from_address['EU']; | |
} | |
elseif( ! empty($ship_from_address['US']) ) { | |
$request['Shipment']['Shipper'] = $ship_from_address['US']; | |
} | |
foreach( $request['Shipment']['package'] as $key => $package ) { | |
if( isset($package['Package']) ) { | |
if( $units == 'metric' && strtoupper($request['Shipment']['package'][$key]['Package']['PackageWeight']['UnitOfMeasurement']['Code']) != 'KGS' ) { | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['UnitOfMeasurement']['Code'] = 'CM'; | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Length'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Length'], 'CM' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Width'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Width'], 'CM' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Height'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Height'], 'CM' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['PackageWeight']['UnitOfMeasurement']['Code'] = 'KGS'; | |
$request['Shipment']['package'][$key]['Package']['PackageWeight']['Weight'] = number_format( wc_get_weight( $request['Shipment']['package'][$key]['Package']['PackageWeight']['Weight'], 'KGS' ), 2, '.', ''); | |
} | |
elseif( $units == 'imperial' && strtoupper($request['Shipment']['package'][$key]['Package']['PackageWeight']['UnitOfMeasurement']['Code']) != 'LBS' ) { | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['UnitOfMeasurement']['Code'] = 'IN'; | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Length'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Length'], 'IN' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Width'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Width'], 'IN' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['Dimensions']['Height'] = number_format( wc_get_dimension( $request['Shipment']['package'][$key]['Package']['Dimensions']['Height'], 'IN' ), 2, '.', ''); | |
$request['Shipment']['package'][$key]['Package']['PackageWeight']['UnitOfMeasurement']['Code'] = 'LBS'; | |
$request['Shipment']['package'][$key]['Package']['PackageWeight']['Weight'] = number_format( wc_get_weight( $request['Shipment']['package'][$key]['Package']['PackageWeight']['Weight'], 'LBS' ), 2, '.', ''); | |
} | |
} | |
} | |
return $request; | |
} | |
} | |
/** | |
* Snippet to Change UPS account based on destination country on cart and checkout page. | |
*/ | |
add_filter( 'wf_ups_shipment_settings', 'wf_ups_change_origin_address_and_unit'); | |
if( ! function_exists('wf_ups_change_origin_address_and_unit') ) { | |
function wf_ups_change_origin_address_and_unit( $settings ) { | |
$alternate_accounts = array( | |
'US' => 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 | |
'units' => 'imperial' // imperial for LBS / IN and metric for KGS / CM | |
), | |
'EU' => array( | |
'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' => 'Atlanta', // Origin City | |
'origin_state' => 'BH', // Origin State | |
'origin_country' => 'FR', // Origin Country | |
'units' => 'metric' // imperial for LBS / IN and metric for KGS / CM | |
) | |
); | |
if( is_cart() || is_checkout() ) { | |
if( in_array( strtoupper(WC()->customer->get_shipping_country()), WC()->countries->get_european_union_countries() ) ) { | |
if( ! empty($alternate_accounts['EU']['shipper_number']) ) $settings['shipper_number'] = $alternate_accounts['EU']['shipper_number']; | |
if( ! empty($alternate_accounts['EU']['user_id']) ) $settings['user_id'] = $alternate_accounts['EU']['user_id']; | |
if( ! empty($alternate_accounts['EU']['password']) ) $settings['password'] = $alternate_accounts['EU']['password']; | |
if( ! empty($alternate_accounts['EU']['access_key']) ) $settings['access_key'] = $alternate_accounts['EU']['access_key']; | |
if( ! empty($alternate_accounts['EU']['origin_addressline']) ) $settings['origin_addressline'] = $alternate_accounts['EU']['origin_addressline']; | |
if( ! empty($alternate_accounts['EU']['origin_postcode']) ) $settings['origin_postcode'] = $alternate_accounts['EU']['origin_postcode']; | |
if( ! empty($alternate_accounts['EU']['origin_city']) ) $settings['origin_city'] = $alternate_accounts['EU']['origin_city']; | |
if( ! empty($alternate_accounts['EU']['origin_state']) ) $settings['origin_state'] = $alternate_accounts['EU']['origin_state']; | |
if( ! empty($alternate_accounts['EU']['origin_country']) ) $settings['origin_country'] = $alternate_accounts['EU']['origin_country']; | |
if( ! empty($alternate_accounts['EU']['units']) ) $settings['units'] = $alternate_accounts['EU']['units']; | |
} | |
elseif( strtoupper(WC()->customer->get_shipping_country()) == 'US' ) { | |
if( ! empty($alternate_accounts['US']['shipper_number']) ) $settings['shipper_number'] = $alternate_accounts['US']['shipper_number']; | |
if( ! empty($alternate_accounts['US']['user_id']) ) $settings['user_id'] = $alternate_accounts['US']['user_id']; | |
if( ! empty($alternate_accounts['US']['password']) ) $settings['password'] = $alternate_accounts['US']['password']; | |
if( ! empty($alternate_accounts['US']['access_key']) ) $settings['access_key'] = $alternate_accounts['US']['access_key']; | |
if( ! empty($alternate_accounts['US']['origin_addressline']) ) $settings['origin_addressline'] = $alternate_accounts['US']['origin_addressline']; | |
if( ! empty($alternate_accounts['US']['origin_postcode']) ) $settings['origin_postcode'] = $alternate_accounts['US']['origin_postcode']; | |
if( ! empty($alternate_accounts['US']['origin_city']) ) $settings['origin_city'] = $alternate_accounts['US']['origin_city']; | |
if( ! empty($alternate_accounts['US']['origin_state']) ) $settings['origin_state'] = $alternate_accounts['US']['origin_state']; | |
if( ! empty($alternate_accounts['US']['origin_country']) ) $settings['origin_country'] = $alternate_accounts['US']['origin_country']; | |
if( ! empty($alternate_accounts['US']['units']) ) $settings['units'] = $alternate_accounts['US']['units']; | |
} | |
} | |
return $settings; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment