Let's say you have a product for which you just want to show the Free Shipping option or the Flat Rate shipping option, you can easuly do it using the following filters.
add_filter( 'woocommerce_shipping_flat_rate_is_available', function( $is_available ) {
// set the product ids that are eligible
$eligible = array( '1711' );
// get cart contents
$cart_items = WC()->cart->get_cart();
// loop through the items looking for one in the eligible array
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $eligible ) ) {
return false; // disabling flat rate shipping
}
}
// nothing found return the default value
return $is_available;
} );
add_filter( 'woocommerce_shipping_free_shipping_is_available', function( $is_available ) {
// set the product ids that are eligible
$eligible = array( '1711' );
// get cart contents
$cart_items = WC()->cart->get_cart();
// loop through the items looking for one in the eligible array
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $eligible ) ) {
return true;
}
}
// nothing found return the default value
return $is_available;
} );