skip to Main Content

I have created a coupon code "Tiendas" for double x2 shipping price and disable free default store shipping (orders > 50€).

Also, the coupon enables free shipping but increase order value to >250€.

I have 3 shipping methods on my store:

  • flat_rate:1
  • flat_rate:7
  • Free_shipping

When coupon is enabled, flat_rate:7 and free shipping must be hidden. And flat_rate:1 should be visible and its shipping cost x2 (4.80 € x 2= 9.60 €)

Only works if i type flat_rate, instead flat_rate:1, but hide all shipping methods instead one.

Based on "https://stackoverflow.com/questions/52912181/applied-coupons-disable-free-shipping-conditionally-in-woocommerce/52913005#52913005" answer thread, here is my code attempt:

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal      = 250; // Minimal subtotal allowing free shipping

    // Get needed cart subtotals
    $subtotal_excl_tax = WC()->cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
    $discount_excl_tax = WC()->cart->get_discount_total();
    $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();

    // Calculating the discounted subtotal including taxes
    $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;

    $applied_coupons   = WC()->cart->get_applied_coupons();
    if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){

            // Set 2x cost"
            if( $rate->method_id === 'flat_rate:1' ){
                // Set 2x of the cost
                $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;}
 
            // Disable "flat_rate:7"
            if( $rate->method_id === 'flat_rate:7'  ){
                unset($rates[$rate_key]);

 
            // Disable "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);
 
                
            }
        }
    }
    }
    return $rates;
    }

2

Answers


  1. Actually you are there, all you have to do is to compare $rate_key instead of $rate->mothod_id

    You code should look like this:

    if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
            foreach ( $rates as $rate_key => $rate ){
    
                // Set 2x cost"
                if( $rate_key === 'flat_rate:1' ){
                    // Set 2x of the cost
                    $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;
                }
    
                // Disable "flat_rate:7"
                if( $rate_key === 'flat_rate:7'  ){
                    unset($rates[$rate_key]);
                }
    
    
                // Disable "Free shipping"
                if( 'free_shipping' === $rate_key  ){
                    unset($rates[$rate_key]);
                }
            }
        }
    

    Or, a bit simplier:

    if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
      unset($rates['flat_rate:7']);
      unset($rates['free_shipping']);
    
      foreach ( $rates as $rate_key => $rate ){
                if( $rate_key === 'flat_rate:1' ) $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;                 // Set 2x of the cost
      }
    }
    
    Login or Signup to reply.
  2. Note1: Correct the typo! o to e

    //$rate->mothod_id
      $rate->method_id
    

    Note2: For the free method

    if( 'free_shipping' === $rate->method_id  ){
       unset($rates[$rate_key]);
    }
    

    or: with index:

    if( 'free_shipping:7' === $rate_key  ){
       unset($rates[$rate_key]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search