title: Managing conditional coupons and additional fees on cart/checkout Woocommerce
body: <p>I am having problems in managing contemporarly disocunt coupons and custom additional fields on the cart and checkout page.</p>
  
  <p>I created a function that adds a discount only if a product is present in the cart with more than a variation and the discount is applied only on the second (or third) variation, NOT on the first one. You can find my final code here <a href="https://stackoverflow.com/questions/40833494/woocommerce-conditional-part-cart-discount-on-the-same-product-present-with-1">Woocommerce conditional part cart discount on the same product present with &gt; 1 variation</a></p>
  
  <p>However I had to go further on, as I have a particular tricky situation. Normally the condition described above cannot be applied when other discounts are in the cart (i.e. standard discount coupons). I hence added these two lines just after getting the cart</p>
  
  <pre><code>$has_coupons = count(WC()-&gt;cart-&gt;applied_coupons)&gt;0?true:false;
  
      if(!$has_coupons) {
      //here goes the function that I did
      } else return;
  </code></pre>
  
  <p>Everything is fine, and it works. However there is a particular situation in which I have to apply a coupon to the cart (not %) that detracts to teh whole cart a specific amount. This particular coupon CAN go with the discount created in the function I wrote!</p>
  
  <pre><code>  /* get coupon code*/
  function get_coupon_code() {
      global $woocommerce;
      if ( !empty( $woocommerce-&gt;cart-&gt;applied_coupons ) ) {
           $my_coupon_code = $woocommerce-&gt;cart-&gt;get_coupons() ;
           foreach($my_coupon_code as $coupon){
  
              if ( $post = get_post( $coupon-&gt;id ) ) {
                      $coupon_code= $coupon-&gt;code;
              }
          }
      }
      return $coupon_code;
  }  
    /* get coupon amount*/
  function get_coupon_amount() {
      global $woocommerce;
      if ( ! empty( $woocommerce-&gt;cart-&gt;applied_coupons ) ) {
           $my_coupon_amount = $woocommerce-&gt;cart-&gt;get_coupons() ;
           foreach($my_coupon_amount as $coupon){
                          $coupon_amount =$coupon-&gt;coupon_amount;
          }
      }
      return $coupon_amount;
  } 
  
  function cart_discount() {
  
      if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
          return;
      global $woocommerce;
      //get the code and amount of the coupon applied
      $coupon_code =  get_coupon_code();
      $coupon_amount = get_coupon_amount();
      //check if the code corresponds to the coupon exception I want to accept 
      if ($coupon_code == 'wa-1'){
          //if it is the code I need to apply, then I remove it from the cart
          WC()-&gt;cart-&gt;remove_coupon($coupon_code);
            //I use the amount to add it as additional fee at negative 
            $coupon_discount = -($coupon_amount);
            //I add the ex-coupon amount to an additional field
            $discount_agency_text = __( 'Detracted agency fee', 'woocommerce' );
            WC()-&gt;cart-&gt;add_fee( $discount_agency_text, $coupon_discount, false );
        }
    //and following the function previously mentioned
  
        $cart = WC()-&gt;cart-&gt;get_cart();
  
        $has_coupons = count(WC()-&gt;cart-&gt;applied_coupons)&gt;0?true:false;
        //check if there are other coupons or if the coupon applied are the case I want to pass through my function
        if(!$has_coupons || $coupon == 'wa-1')          {
  
          //here follows the strings to apply a 10% discount on the second (third) variation of the product and not on the first one
  
              foreach($cart as $cart_item_key =&gt; $values) { 
                  $product_id = $values['product_id']; // product ID
                  $variation_id = $values['variation_id']; // product quantity
                  $cart_lines_total = $values["line_total"];
                  $cart_lines_quantity = $values["quantity"];
  
                      //product id to be considered for this discount
                      //$product = 1394 = Spedizioni CSR eng
                      //$product = 1389 = Spedizioni IDP eng
                      //$product = 13888 = Spedizioni CSR ita
                      //$product = 13910 = Spedizioni IDP ita
  
                  if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
                  {
                      $cart_array []= array( $product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
                  }
              }
  
                  $conteggio = count($cart_array); //counts the number of items in teh cart 
                  // percent is 5%
                  $percent = -0.10;
  
              if ($conteggio &lt; 3 &amp;&amp; $cart_array[0][0] == $cart_array[1][0])
              { 
                  $discount = $percent *  $cart_array[1][3];
                  $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                  WC()-&gt;cart-&gt;add_fee( $discount_text, $discount, false );
              }
              if ($conteggio &lt; 4 &amp;&amp; $cart_array[0][0] == $cart_array[1][0] &amp;&amp; $cart_array[0][0] == $cart_array[2][0])
              {
                  $discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]);
                  $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                  WC()-&gt;cart-&gt;add_fee( $discount_text, $discount, false );
              }
              if ($conteggio &lt; 5 &amp;&amp; $cart_array[0][0] == $cart_array[1][0] &amp;&amp; $cart_array[0][0] == $cart_array[2][0] &amp;&amp; $cart_array[0][0] == $cart_array[3][0])
              {
                  $discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]) + ($percent * $cart_array[3][3]);
                  $discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
                  WC()-&gt;cart-&gt;add_fee( $discount_text, $discount, false );
              }
          } else return;
  }
  
  
  add_action( 'woocommerce_cart_calculate_fees','cart_discount' );
  </code></pre>
  
  <p>The problem I have is that part concernign the management of teh discount coupon doesn't work. It gets the $coupon_code and $coupon_amount, but doesn't add it to the additional fields section. </p>
  
  <p>Anybody can help me please?</p>