Skip to content

Instantly share code, notes, and snippets.

@robin-scott
Last active November 21, 2018 14:05
Show Gist options
  • Save robin-scott/1943b7802085ee4babca3bb2edf35be3 to your computer and use it in GitHub Desktop.
Save robin-scott/1943b7802085ee4babca3bb2edf35be3 to your computer and use it in GitHub Desktop.
WooCommerce - Add extra weight to all cart items in a specific shipping class - Find out more here -> https://silicondales.com/tutorials/woocommerce/add-additional-shipping-weight-to-cart-items-in-a-shipping-class/
// By Robin Scott of Silicon Dales
// Add 6oz extra weight to all cart items in specific shipping class
// More information here: https://silicondales.com/tutorials/woocommerce/add-additional-shipping-weight-to-cart-items-in-a-shipping-class/
add_action( 'woocommerce_before_calculate_totals', 'rscott_add_custom_weight', 10, 1);
function rscott_add_custom_weight( $cart_object ) {
if ( (is_admin() && ! defined( 'DOING_AJAX' ) ) || $cart_object->is_empty() )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$additionalweight = 6;
foreach ( $cart_object->get_cart() as $cart_item ) {
$shipping_class = $cart_item['data']->get_shipping_class();
if ($shipping_class == 'shipping-class-slug' && !empty($original_weight)){
//get the current weight
$original_weight = $cart_item['data']->get_weight();
// calculate new weight
$new_weight = $original_weight + $additionalweight;
// Set new weight
$cart_item['data']->set_weight( $new_weight );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment