Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active November 29, 2017 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WooForce/405d18ec45ddc3385b78a0dc7b2a6c53 to your computer and use it in GitHub Desktop.
Save WooForce/405d18ec45ddc3385b78a0dc7b2a6c53 to your computer and use it in GitHub Desktop.
Add additional fee to shipping options based on Shipping Class
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