Set Free Shipping as Default and Cart Total Should Not Include The Shipping Fee in WooCommerce

29 Views Asked by At

I followed this answer and it worked:

Set Free Shipping as Default in WooCommerce

However, the total amount in the cart still displays the value that includes the flat rate fee.

How do you modify it to exclude the fee from the total?

I've tried this, but it's not working

function woocommerce_cart_totals_shipping_cost( $total ) {
  if ( WC()->cart->needs_shipping() && WC()->session->get( 'chosen_shipping_methods' ) ) {
    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
    $free_shipping_id = 'free_shipping'; // Replace with your actual free shipping method ID

    foreach ( $chosen_shipping_rates as $rate_id ) {
      if ( $rate_id === $free_shipping_id ) {
        return $total; // No change if free shipping is chosen
      }
    }
  }

  return $total + WC()->cart->get_shipping_total(); // Add shipping cost for other methods
}

add_filter( 'woocommerce_cart_totals_shipping_cost', 'woocommerce_cart_totals_shipping_cost', 10, 1 );
0

There are 0 best solutions below