Skip to content

Instantly share code, notes, and snippets.

@gthayer
Last active December 12, 2016 16:15
Show Gist options
  • Save gthayer/399801813e5a36af9b63fd88df590086 to your computer and use it in GitHub Desktop.
Save gthayer/399801813e5a36af9b63fd88df590086 to your computer and use it in GitHub Desktop.
add_filter( 'woocommerce_shipping_packages', 'free_shipping_wreaths' );
function free_shipping_wreaths( $packages ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
foreach ( $packages as $i => $package ) {
$chosen_method = false;
if ( ! empty( $chosen_methods[ $i ] ) ) {
$chosen_method = $chosen_methods[ $i ];
}
if ( sizeof( $package['rates'] ) > 0 ) {
if ( $chosen_method ) {
foreach ($package as $k => $items) {
if ( ! is_array( $items ) ) {
continue;
}
foreach ($items as $key => $item) {
if ( ! is_array( $item ) ) {
continue;
}
if ( ! empty( $item['product_id']) ) {
if ( has_term( 'category', 'product_cat', $item['product_id'] ) ) {
$discount = 18.95;
// switch ( $item['shipping_class'] ) {
// case 'ground':
// $discount = 18.95;
// break;
// case 'overnight-upgrade':
// $discount = 28.95;
// break;
// default:
// $discount = 0;
// break;
// }
$packages[$i]['rates'][ $chosen_method ]->cost -= $discount;
}
}
}
}
}
}
}
return $packages;
}
add_action( 'woocommerce_after_shipping_rate', 'test', 20, 2 );
function test( $method, $index ) {
if ( $method->cost == 0 ) {
echo "- Free Shipping!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment