Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vanbo/af185f82de890a758d6e2513a89dbfa3 to your computer and use it in GitHub Desktop.
Save vanbo/af185f82de890a758d6e2513a89dbfa3 to your computer and use it in GitHub Desktop.
WooCommerce: Allow free shipping when certain product is in cart
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'prefix_is_available_for_certain_products', 10, 3 );
/**
* @param bool $is_available
* @param array $package Shipping package.
* @param WC_Shipping_Free_Shipping $shipping_class
*
* @return bool
*/
function prefix_is_available_for_certain_products( $is_available, $package, $shipping_class ) {
// Bail, if already allowed
if ( $is_available ) {
return $is_available;
}
// Add your product IDs
$product_map = array(
50,
47,
44
);
foreach ( $package['contents'] as $hash => $data ) {
$id = 0 < $data['variation_id'] ? $data['variation_id'] : $data['product_id'];
if ( in_array( $id, $product_map ) ) {
return true;
}
}
return $is_available;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment