title: WooCommerce - Buy one Get one Free
body: <p>I have created a code in WooCommerce Coupon Codes called "2FOR1WOW" and added this code to functions.php but it's not working. The ticket gets applied and a confirmation message says it's ok but the total is not reduced.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <pre><code>// Hook before calculate fees - "Buy 2 get cheapest free" coupon
  add_action('woocommerce_cart_calculate_fees' , 'buy2_coupon');
  
  /**
   * Add discount for "Buy 2 get cheapest free" coupon
   * @param WC_Cart $cart
   */
  
  function buy2_coupon( WC_Cart $cart ){
  
      // add the coupons here
      $buy2_coupons = array('2FOR1WOW', 'anothercouponcode');
  
      // return if cart has less than 2 items
      if( $cart-&gt;cart_contents_count &lt; 2 ){
          return;
      }
  
      $applied_coupons = $cart-&gt;get_applied_coupons();    
      $matches = array_intersect($buy2_coupons, $applied_coupons);
  
      // return if no coupon matches
      if (empty($matches)) return;
  
      // loop through the items in cart to find the cheapest
      foreach ( $cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
          $_product = $values['data'];
          $product_price[] = $_product-&gt;get_price_including_tax();
      }
  
      $cheapest = min($product_price);
  
      $cart-&gt;add_fee( 'Coupon 2FOR1WOW', -$cheapest, true, 'standard' );
  }
  </code></pre>