1

I am trying to remove the free shipping option in Woocommerce when someone uses a specific coupon code. I found this question which is very relevant to my question. The answer bellow seems really close to what I am looking for. I am new to php and trying to figure out where I would add an id for the coupon code I want to exclude.

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
  $applied_coupons = WC()->session->get('applied_coupons', array());

  if (!empty($applied_coupons)) {
    $free_shipping_id = 'free_shipping:2';
    unset($packages[0]['rates'][ $free_shipping_id ]);
  }

  return $packages;
});

This is my first question on stack over flow but this site has helped me so much with so many issues. Forgive me if I am asking too much. Thanks in advance for any help/guidance.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 2
    Welcome to Stackoverflow. Please remember to actually include the question you have explicitly [in your post](/help/how-to-ask). Right now, all you're saying is you want to do something, and you found code, but that doesn't constitute a problem: you want to do something and found code, so right now it looks like you have everything you need to get started on rewriting what you found to suit your needs, rather than there being a problem now that you've tried to adapt that code. SO is here to help you solve programming problems, but that does require properly describing the problem =) – Mike 'Pomax' Kamermans Sep 18 '20 at 16:08
  • 2
    (and note that Stackoverflow is not a general help forum, "I don't know how to do this" is not a programming problem, but a skills problem, and out of scope for Stackoverflow. "I thought I knew how to do this, so I wrote this code: ..., but instead of doing Y, it seems to do Z, and even after debugging and trying U, V, and W, I can't figure out why" is a programming problem) – Mike 'Pomax' Kamermans Sep 18 '20 at 16:09
  • 1
    having looked at https://developer.wordpress.org/reference/functions/add_filter/ I would say that you need to tell the add_filter() function, how many arguments you accept back. That seems to be why in the question you mentioned the marked answer adds the filter like this: add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 ); – j4g0 Sep 18 '20 at 16:12

1 Answers1

2

Use instead woocommerce_package_rates filter hook. In the code below you will set the related coupon codes that will hide the free shipping method:

add_filter( 'woocommerce_package_rates', 'hide_free_shipping_method_based_on_coupons', 10, 2 );
function hide_free_shipping_method_based_on_coupons( $rates, $package )
{
    $coupon_codes       = array('summer'); // <== HERE set your coupon codes
    $applied_coupons    = WC()->cart->get_applied_coupons(); // Applied coupons
    
    if( empty($applied_coupons) )
        return $rates;

    // For specific applied coupon codes
    if( array_intersect($coupon_codes, $applied_coupons) ) {
        foreach ( $rates as $rate_key => $rate ) {
            // Targetting "Free shipping"
            if ( 'free_shipping' === $rate->method_id ) {
                unset($rates[$rate_key]); // hide free shipping method
            }
        }
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Clearing shipping caches:

  • You will need to empty your cart, to clear cached shipping data
  • Or In shipping settings, you can disable / save any shipping method, then enable back / save.
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you so much for getting back to me! I appreciate the help/insight trying to put this in my functions.php folder and I receive the error 'syntax error, unexpected 'return' (T_RETURN)' referring to the 8th line. – Josh Fuller Sep 19 '20 at 17:23
  • @JoshFuller Just a missing bracket sorry… Updated. – LoicTheAztec Sep 19 '20 at 21:24