Custom functions

Hide checkout live rates if country = "Bulgaria"

if ( ! function_exists( 'iben_ups_skip_country_front_rates' ) ) {
    
    function iben_ups_skip_country_front_rates($default, $args){
        
        if (isset($args['destination']['country']) && $args['destination']['country'] == 'BG') return true;

        return $default;
    }

add_filter('iben_ups_filter_liverate_checkout_disable_request_rate', 'iben_ups_skip_country_front_rates', 10, 2);
}

Change default account id for frontend live rates

if ( ! function_exists( 'iben_ups_change_default_account_id_front_rates' ) ) {
    function iben_ups_change_default_account_id_front_rates($account_id){
        // account ID
    	return 20;
    }
    
add_filter('iben_ups_filter_live_rates_default_account', 'iben_ups_change_default_account_id_front_rates');
}

Change default weight to 0.5 for all products if didn't set or equal to zero

if ( ! function_exists( 'iben_ups_change_default_weight' ) ) {
    function iben_ups_change_default_weight($default, $args){

        return 0.5;
    }
    
add_filter('iben_ups_filter_get_package_default_weight', 'iben_ups_change_default_weight', 10, 2);
}

Hide UPS Standard ( ID:11 ) if country is France and total weight > 10kg

if ( ! function_exists( 'iben_ups_disable_service_conditions' ) ) {
	function iben_ups_disable_service_conditions($boolean, $ups_id, $args){

		if (!isset($args['destination']['country']) || !isset($args['total_weight']) ) return true;

		if ($ups_id == '11' && $args['destination']['country'] == 'FR' && $args['total_weight'] > 10){
			return false;
		}

		return true;
	}

add_filter('iben_ups_filter_liverate_checkout_disable_service', 'iben_ups_disable_service_conditions', 10, 3);
}

Hide shipment creation box for specific countries

if ( ! function_exists( 'iben_ups_except_display_countries' ) ) {
	function iben_ups_except_display_countries(){
		return array('US', 'GR');
	}
add_filter('iben_ups_filter_order_except_display_countries', 'iben_ups_except_display_countries');
}

Override final UPS rate price. Add 24% percent

if ( ! function_exists( 'iben_ups_override_final_price' ) ) {
	function iben_ups_override_final_price($ups_price, $args){
		
		return $ups_price * 1.24;
	}
add_filter('iben_ups_filter_liverate_checkout_price', 'iben_ups_override_final_price', 10, 2);
}

Override Fallback price

If country is Bulgaria and total weight <= 1 price = 15 else price = 50 else if other country return default fallback price

if ( ! function_exists( 'iben_ups_override_fallback_price' ) ) {
	function iben_ups_override_fallback_price($default_fallback_price, $args){
		
		$total_weight = 0;

		if ( isset( $args['destination']['country'] ) && $args['destination']['country'] == 'BG'){
			
			if ( isset( $args['packages'] ) && !empty($args['packages']) ){
				
				foreach($args['packages'] as $package){
					$total_weight = $total_weight + floatval($package['weight']);
				}
			}

			if ($total_weight <= 1 ){
				return 15;
			}else{
				return 50;
			}

		}

		return $default_fallback_price;
	}
add_filter('iben_ups_filter_live_rates_callback_price', 'iben_ups_override_fallback_price', 10, 2);
}

Change UPS description with order customer notes

add_filter('iben_ups_filter_order_metabox_description', 'iben_custom_filter_order_description', 20, 2);
function iben_custom_filter_order_description($default, $filter_args){

    $order_id = $filter_args['order_id'];
    // Order Object
    $order = wc_get_order($order_id);
    if (!is_object($order)) return $default;

    $order_notes = $order->get_customer_note();

    return $order_notes;
}

Add Order ID in the description field

This may not work for USA domestic shipments. Alternatively, you can use the next function

add_filter('iben_ups_filter_order_metabox_description', 'iben_custom_filter_order_id_description', 20, 2);
function iben_custom_filter_order_id_description($default, $filter_args){
	$order_id = $filter_args['order_id'];
	// Order Object
	$order = wc_get_order($order_id);
	if (!is_object($order)) return $default;
	
	$get_order_number = 'Order ID '.$order->get_order_number();
	return $get_order_number;
}

Change the default reference1 of the label with the order ID

// CHANGE CHECKBOX TO TRUE AS DEFAULT VALUE
// Enabled checkbox by default for USA domestics
add_filter( 'iben_ups_filter_order_metabox_package_enable_reference1', '__return_true' ); 
// Enabled checkbox by default for international
add_filter( 'iben_ups_filter_order_metabox_enable_reference1', '__return_true' ); 

// CHANGE THE DEFAULT DROPDOWN VALUE
function iben_change_default_reference_value_with_po($default, $args){
	return 'PO';
}
// for USA domestics
add_filter('iben_ups_filter_order_metabox_package_reference_type1', 'iben_change_default_reference_value_with_po', 10, 2);
// for international
add_filter('iben_ups_filter_order_metabox_reference_type1', 'iben_change_default_reference_value_with_po', 10, 2);

// ADD ORDER ID AS DEFAULT VALUE IN THE TEXT FIELD
function iben_change_default_reference_value_with_orderid($default, $args){
	$order_id = $args['order_id'];
	// Order Object
	$order = wc_get_order($order_id);
	if (!is_object($order)) return $default;
	
	$get_order_number = 'Order ID '.$order->get_order_number();
	return $get_order_number;

}


// Fire filter for USA domestics
add_filter('iben_ups_filter_order_metabox_package_reference_value1', 'iben_change_default_reference_value_with_orderid', 10, 2);
// Fire filter  for international
add_filter('iben_ups_filter_order_metabox_reference_value1', 'iben_change_default_reference_value_with_orderid', 10, 2);

Change the default reference1 of the label with product names

// CHANGE CHECKBOX TO TRUE AS DEFAULT VALUE
// Enabled checkbox by default for USA domestics
add_filter( 'iben_ups_filter_order_metabox_package_enable_reference1', '__return_true' ); 
// Enabled checkbox by default for international
add_filter( 'iben_ups_filter_order_metabox_enable_reference1', '__return_true' ); 

// CHANGE THE DEFAULT DROPDOWN VALUE
function iben_change_default_reference_type1($default, $args){
	return 'AJ';
}
// for USA domestics
add_filter('iben_ups_filter_order_metabox_package_reference_type1', 'iben_change_default_reference_type1', 10, 2);
// for international
add_filter('iben_ups_filter_order_metabox_reference_type1', 'iben_change_default_reference_type1', 10, 2);

// ADD PRODUCT NAME AS DEFAULT VALUE IN THE TEXT FIELD
function iben_change_default_reference_value1($default, $args){
	$default_text_description = '';
	if ( function_exists( 'iben_ups_get_product_names_from_order_id' ) ) {
		$default_text_description = iben_ups_get_product_names_from_order_id($args['order_id']);
	}
	
	return $default_text_description;

}
// Fire filter for USA domestics
add_filter('iben_ups_filter_order_metabox_package_reference_value1', 'iben_change_default_reference_value1', 10, 2);
// Fire filter  for international
add_filter('iben_ups_filter_order_metabox_reference_value1', 'iben_change_default_reference_value1', 10, 2);

Change order First name & Last name

By default, plugins fill first the Last name and then the First name

With the following function, you can revert the sorting

add_filter('iben_ups_filter_order_metabox_attention_name', 'iben_custom_change_order_name_lastname', 10, 3);
function iben_custom_change_order_name_lastname($attentionname, $filter_args2, $filter_args){
	
	return $filter_args2['first_name']. ' '.$filter_args2['last_name'];
}

Last updated