Last active
November 29, 2017 05:12
-
-
Save WooForce/405d18ec45ddc3385b78a0dc7b2a6c53 to your computer and use it in GitHub Desktop.
Add additional fee to shipping options based on Shipping Class
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
add_filter( 'woocommerce_package_rates', 'adjustment_in_rates_of_product_with_shipping_class', 10, 2 ); | |
function adjustment_in_rates_of_product_with_shipping_class( $available_shipping_methods, $package ) { | |
// Shipping class IDs that need to add extra cost | |
$shipping_class_ids = array( | |
0, | |
1, | |
); | |
// Give here extra cost you want add | |
$extra_cost = 10; | |
$shipping_services = array( | |
'wf_shipping_ups:01', | |
'wf_shipping_ups:02', | |
'wf_shipping_ups:03', | |
'wf_shipping_ups:07', | |
'wf_shipping_ups:08', | |
'wf_shipping_ups:11', | |
'wf_shipping_ups:12', | |
'wf_shipping_ups:13', | |
'wf_shipping_ups:14', | |
'wf_shipping_ups:59', | |
'wf_shipping_ups:54', | |
'wf_shipping_ups:65', | |
'wf_shipping_ups:92', | |
'wf_shipping_ups:93', | |
'wf_shipping_ups:94', | |
); | |
$shipping_class_exists = false; | |
foreach(WC()->cart->get_cart_contents() as $key => $values) { | |
if ( in_array($values['data']->get_shipping_class_id() , $shipping_class_ids) ) { | |
$shipping_class_exists = true; | |
break; | |
} | |
} | |
if ($shipping_class_exists) { | |
foreach ($available_shipping_methods as $key => $value) { | |
$available_shipping_methods[$key]->cost += $extra_cost; | |
} | |
} | |
return $available_shipping_methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment