Last active
September 20, 2019 14:10
-
-
Save xadapter/b52eae8ba6d8f8d60513f31bb4cbc630 to your computer and use it in GitHub Desktop.
Snippet to add handling charge in percent to WooCommerce Shipping Cost based on WooCommerce Shipping method and WooCommerce Shipping Zone. PluginHive Plugins supported - https://www.pluginhive.com/product/multiple-carrier-shipping-plugin-woocommerce/, https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/, https://w…
This file contains hidden or 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
/** | |
* Snippet to add handling charge in percent to to shiiping cost based on shipping method and shipping zone. Use * to define for all the remaining zone. | |
* Created at : 09 July 2018 | |
* Updated at : 18 July 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
* Gist Link : https://gist.github.com/xadapter/b52eae8ba6d8f8d60513f31bb4cbc630 | |
*/ | |
add_filter( 'woocommerce_package_rates',function( $shipping_rates ){ | |
$handling_fee_based_on_zone = array( | |
array( | |
'zone_ids' => array( 11, 5, 7), // Zone id array | |
'adjustment' => 10, // Adjustment in percent | |
), | |
array( | |
'zone_ids' => array( 1, 2, 4 ), | |
'adjustment' => 100, | |
), | |
'*' => array( // For all other remaining zone | |
'zone_ids' => array('rest_shipping_zones'), | |
'adjustment' => 200, | |
), | |
); | |
$shipping_method = array('wf_woocommerce_shipping_pro', 'flat_rate'); // Shipping methods to which handling fee need to be added | |
$destination = array( | |
'country' => WC()->customer->get_shipping_country(), | |
'state' => WC()->customer->get_shipping_state(), | |
'postcode' => WC()->customer->get_shipping_postcode(), | |
); | |
// Get the matching zone | |
$zone = WC_Shipping_Zones::get_zone_matching_package( | |
array( | |
'destination' => array( | |
'country' => $destination['country'], | |
'state' => $destination['state'], | |
'postcode' => $destination['postcode'], | |
) | |
) | |
); | |
$zone_id = $zone->get_id(); | |
$selected_key = null; | |
if( ! empty($zone_id) ) { | |
foreach( $handling_fee_based_on_zone as $key => $data ) { | |
if( in_array($zone_id, $data['zone_ids']) ) { | |
$selected_key = $key; | |
} | |
} | |
} | |
if( $selected_key === null ){ | |
$selected_key = '*'; | |
} | |
if( ! empty($handling_fee_based_on_zone[$selected_key]) ) { | |
foreach( $shipping_rates as $shipping_rate ) { | |
$method_id = $shipping_rate->get_method_id(); | |
if( in_array( $method_id, $shipping_method) ) { | |
$cost = (float) $shipping_rate->get_cost(); | |
$new_cost = $cost + ( $cost * $handling_fee_based_on_zone[$selected_key]['adjustment'] / 100 ); | |
$shipping_rate->set_cost($new_cost); | |
} | |
} | |
} | |
return $shipping_rates; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment