Skip to content

Instantly share code, notes, and snippets.

@WooForce
Created August 11, 2016 08:12
Show Gist options
  • Save WooForce/adcea3fccf892455a99e5c79ee41efd0 to your computer and use it in GitHub Desktop.
Save WooForce/adcea3fccf892455a99e5c79ee41efd0 to your computer and use it in GitHub Desktop.
Adjusting shipping rates based on product 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 with extra costs.
$shipping_class_and_price = array(
13 => 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',
);
$max_price = 0;
foreach(WC()->cart->cart_contents as $key => $values) {
foreach ($shipping_class_and_price as $class_id => $price) {
if( $values['data']->get_shipping_class_id()==$class_id ){
$max_price = $max_price > $price ? $max_price : $price;
}
}
}
if ($max_price > 0) {
foreach ($available_shipping_methods as $key => $value) {
if( in_array( $key, $shipping_services ) ){
$available_shipping_methods[$key]->cost += $max_price;
}
}
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment