title: Add Variable product into cart automatically when cart total is more than 25
body: <p>I want to add automatically one variations product into cart when cart total is become more than 25. The automatically adding variation product price need to set as 0 and also customers cant change the qty or need to disable the Qty filed for free product. I added below code and its working fine only the issue when page reloading or cart updating the the Qty of free product is increasing every time.I only want to sell 1 qty of free product. How to do that ? Code is following below.</p>
  
  <pre><code>function aapc_add_product_to_cart() {
      global $woocommerce;
  
      $cart_total = 25;   
      if ( $woocommerce-&gt;cart-&gt;total &gt;= $cart_total ) {
          if ( ! is_admin()) {
              $free_product_id = 2696; 
              $variation_id = 2697; 
              $arr = array();
              $arr['Color'] = 'Blue';// Product Id of the free product which will get added to cart
              $found      = false;
              //check if product already in cart
              if ( sizeof( WC()-&gt;cart-&gt;get_cart() ) &gt; 0 ) {
                  foreach ( WC()-&gt;cart-&gt;get_cart() as $cart_item_key =&gt; $values ) {
                      $_product = $values['data'];
  
                      if ( $_product-&gt;get_id() == $free_product_id )
                          $found = true;                  
                  }
  
                  // if product not found, add it
                  if ( ! $found )
  
                      WC()-&gt;cart-&gt;add_to_cart( $free_product_id,1,$variation_id,$arr );
              } else {
                  // if no products in cart, add it
  
                  WC()-&gt;cart-&gt;add_to_cart( $free_product_id,1,$variation_id,$arr );
              }        
          }
      }        
  }
  add_action( 'template_redirect', 'aapc_add_product_to_cart' );
  function add_custom_price( $cart_object ) {
      $cart_total           = 25; // If cart total is more than this value.
      $free_product_id    = 2697; // Set price to 0 of this free product.
      $carttotal = 0;
      foreach ( $cart_object-&gt;cart_contents as $key =&gt; $value ) {
          $_product = $value['data'];
          if ( $_product-&gt;get_id() == $free_product_id ){
              continue;
          }
          $carttotal += $value['line_total'];
      }
      if ( $carttotal &gt;= $cart_total ) {
          $custom_price = 0; // This will be your custome price  
          foreach ( $cart_object-&gt;cart_contents as $key =&gt; $value ) {
              $_product = $value['data'];
              if ( $_product-&gt;get_id() == $free_product_id ){
                  $value['data']-&gt;set_price( $custom_price );
              }           
          }
      }   
  }
  
  
  add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
  
  add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
  function hide_quantity_input_field( $args, $product ) {
      // Here set your product IDs in the array
      $product_ids = array(2697);
  
  
      // Handling product variation
      $the_id = $product-&gt;is_type('variation') ? $product-&gt;get_variation_id() : $product-&gt;get_id();
  //var_dump($the_id);
      // Only on cart page for a specific product category
      if( is_cart() &amp;&amp; in_array( $the_id, $product_ids ) ){
          $input_value = $args['input_value'];
          $args['min_value'] = $args['max_value'] = $input_value;
      }
      return $args;
  }
  </code></pre>