I am attempting to remove a shipping option if a coupon is applied but only on a specific product. I have tried the following but it is not working specific to a product:

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
  if ( ! empty( $applied_coupons ) && is_single(8)) {
    $free_shipping_id = 'flat_rate:1';
    unset($packages[0]['rates'][ $free_shipping_id ]);
  return $packages;
} );

Instead of remove shipping, you can apply Free shipping for specific products. You even don't need any code for this.

Using the free shipping method will not allow you to specify certain products since it does not have the options to accomplish that such as shipping classes. Instead, we are going to use the Flat Rate shipping method.

  1. Go to the shipping zone, and add the Flat Rate shipping zone to it.
  2. Then, go to WooCommerce ⇒ Settings ⇒ Shipping ⇒ Shipping Classes, and create a new class and name it ‘Free Shipping‘.
  3. After that, go to the Products page, and select from the table the products you want to set them as free shipping, select the checkboxes, then at the top of the table select Edit from the Bulk Actions, then click apply.
  4. Set the shipping class of the products to ‘Free Shipping’.
  5. Now, go to Shipping Zones and click on the ‘Flat Rate’ method. You’ll be redirected to the method’s settings page.
  6. Change the name of the method to ‘Free Shipping’.
  7. Finally, set the “Free Shipping” Shipping Class Cost option to 0.
  8. Save the changes and now only the products that have the ‘Free Shipping‘ class will have a free shipping.

Credit: https://wpruby.com/woocommerce-set-free-shipping-certain-products/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.