title: How to check if a coupon has granted free shipping in Woocommerce?
body: <p>ORIGINAL QUESTION:</p>
  
  <p>We need to know if a coupon has specifically granted free shipping.</p>
  
  <p>In my test scenario I have two coupons applied to the cart, one with free shipping and one without. The var_dump clearly shows one coupon like this: </p>
  
  <pre><code> ["free_shipping"]=&gt;
     bool(true)
  </code></pre>
  
  <p>However, when using </p>
  
  <pre><code>  echo "&lt;p&gt;Free shipping: ".(int)$coupon-&gt;free_shipping."&lt;/p&gt;";
  </code></pre>
  
  <p>It always displays 0. </p>
  
  <p>I can't figure out what I am doing wrong.</p>
  
  <p>Here's the code:</p>
  
  <pre><code> add_action('woocommerce_before_cart_totals', 'check_free_shipping_on_coupon');
  
  function check_free_shipping_on_coupon() {
  
      global $woocommerce;
  
      if ( ! empty( $woocommerce-&gt;cart-&gt;applied_coupons ) ) {
  
  
          $my_coupon = $woocommerce-&gt;cart-&gt;get_coupons() ;
  
  
          foreach($my_coupon as $coupon){
  
              if ( $post = get_post( $coupon-&gt;id ) ) {
  
                          echo "&lt;p&gt;ID: ".$coupon-&gt;id . "&lt;/p&gt;";
  
                          echo "&lt;p&gt;Code: ".$coupon-&gt;code."&lt;/p&gt;";
  
                          echo "&lt;p&gt;Amount: ".$coupon-&gt;amount."&lt;/p&gt;";
  
                          if ( !empty( $post-&gt;post_excerpt ) ) {
  
                          echo "&lt;p&gt;Excerpt: ".$post-&gt;post_excerpt."&lt;/p&gt;";
  
                          }
  
                          echo "&lt;p&gt;Free shipping: ".(int)$coupon-&gt;free_shipping."&lt;/p&gt;";
  
                          if ( (int)$coupon-&gt;free_shipping == '1'){//I want to do stuff here;
  
                          }
  
                          //Dump everything for reference
                          echo '&lt;pre&gt;' , var_dump($coupon) , '&lt;/pre&gt;';
  
  
              }
          }
      }
  }
  </code></pre>
  
  <p>UPDATE: </p>
  
  <p>I found another question here: <a href="https://stackoverflow.com/questions/48658290/set-all-shipping-methods-cost-to-zero-for-a-free-shipping-coupon-in-woocommerce">Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce</a> that had a code snippet that solved my question, but in a very different way. I am still curious why my original code did not work. </p>
  
  <p>Working code:</p>
  
  <pre><code>//Check if a coupon has granted free shipping
  
  add_action('woocommerce_before_cart_totals', 'rnr_check_free_shipping_on_coupon');
  
  function rnr_check_free_shipping_on_coupon() {
  
  $applied_coupons = WC()-&gt;cart-&gt;get_applied_coupons();
  
      foreach( $applied_coupons as $coupon_code ){
  
      $coupon = new WC_Coupon($coupon_code);
  
      if($coupon-&gt;get_free_shipping()){
  
          echo 'You have a free shipping coupon!';
  
      }
  
    }
  
  }
  </code></pre>