Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Created July 13, 2017 22:55
Show Gist options
  • Save WPprodigy/7134a6068d7b3b8737803229e10e1ea5 to your computer and use it in GitHub Desktop.
Save WPprodigy/7134a6068d7b3b8737803229e10e1ea5 to your computer and use it in GitHub Desktop.
Change shipping costs conditionally based on package contents.
add_filter( 'woocommerce_shipping_packages', 'wc_ninja_woocommerce_package_rates', 20 );
function wc_ninja_woocommerce_package_rates( $packages ) {
$is_warm_weather = true;
foreach ( $packages as $package ) {
$has_candy = false;
foreach ( $package['contents'] as $cart_product ) {
if ( has_term( 'clothing', 'product_cat', $cart_product['product_id'] ) ) {
$has_candy = true;
break;
}
}
if ( $is_warm_weather && $has_candy && sizeof( $package['rates'] ) > 0 ) {
foreach ( $package['rates'] as $rate ) {
$rate->cost = $rate->cost + 10;
}
}
}
return $packages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment