Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bolderelements/395a7d5fefa0ba64a85773c5c3834967 to your computer and use it in GitHub Desktop.
Save bolderelements/395a7d5fefa0ba64a85773c5c3834967 to your computer and use it in GitHub Desktop.
Add shipping for surcharge for large items
add_filter( 'woocommerce_package_rates', 'add_shipping_option_base_fee', 10, 2 );
function add_shipping_option_base_fee( $rates, $package ) {
foreach( $rates as $key => $rate ) {
// reset defaults
$highest_length = 0;
// Do not apply base fee if not the right option
if( $rate->id !== 'betrs_shipping:1-1' ) continue;
// find product with greatest length
foreach( $package['contents'] as $key => $values ) {
$item_length = $values[ 'data' ]->get_length();
if( $item_length > $highest_length ) {
$highest_length = $item_length;
}
}
// Add $5 surcharge on orders with items larger than 21cm
if( $highest_length > 21 ) {
$rates[ $key ]->cost += 5.00;
}
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment