title: WooCommerce add upsell product to the cart when product is added
body: <p>I'm trying to implement a custom upsell function into my WooCommerce Theme.
  When an user add a product, I need to add extra products (upsell) to its cart. That mean I have to add an extra parameter to the product which allow me to get the relation product/upsells and delete associated upsells if the product is deleted from the cart.</p>
  
  <p>The user can choose these upsell products via a list of checkbox. Here is an overview of my "content-single-product.php" WooCommerce template:</p>
  
  <pre><code>&lt;form action="&lt;?php echo esc_url( $product-&gt;add_to_cart_url() ); ?&gt;" class="cart" method="post" enctype="multipart/form-data"&gt;
      &lt;?php
  
      // Get Upsells Product ID:
      $upsells = $product-&gt;get_upsells();
  
      if ( sizeof( $upsells ) == 0 ) return;
  
      $meta_query = $woocommerce-&gt;query-&gt;get_meta_query();
  
      // Get Upsells:
      $args = array(
          'post_type'           =&gt; 'product',
          'ignore_sticky_posts' =&gt; 1,
          'no_found_rows'       =&gt; 1,
          'posts_per_page'      =&gt; $posts_per_page,
          'orderby'             =&gt; $orderby,
          'post__in'            =&gt; $upsells,
          'post__not_in'        =&gt; array( $product-&gt;id )
      );
  
      $query = new WP_Query( $args );
  
      if ( $query-&gt;have_posts() ): ?&gt;
          &lt;ul&gt;
          &lt;?php while ( $query-&gt;have_posts() ): $query-&gt;the_post(); ?&gt;
              &lt;label for="upsell_&lt;?php the_ID(); ?&gt;"&gt;&lt;input type="checkbox" name="upsells[]" id="upsell_&lt;?php the_ID(); ?&gt;" value="&lt;?php the_ID(); ?&gt;" /&gt; &lt;?php the_title(); ?&gt;&lt;/label&gt;
          &lt;?php endwhile; ?&gt;
          &lt;/ul&gt;
      &lt;?php endif; ?&gt;
  
      &lt;?php wp_reset_postdata(); ?&gt;
  
      &lt;button type="submit"&gt;Add to cart&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The code above has been simplified..</p>
  
  <p>Then, here is the code I added into functions.php which allow me to add upsells to the cart and add an extra row to the associated product:</p>
  
  <pre><code>/**
   * Add upsells as extra data for added product
   */
  function add_upsells_to_cart( $cart_item_key ) {
      global $woocommerce;
  
      if ( empty( $_REQUEST['upsells'] ) || ! is_array( $_REQUEST['upsells'] ) )
          return;
  
      // Prevent loop
      $upsells = $_REQUEST['upsells'];
      unset( $_REQUEST['upsells'] );
  
      // Add upsell_parent row (if not already existing) for the associated product
      if ( ! $cart_item_data['upsells'] || ! is_array( $cart_item_data['upsells'] ) )
          $cart_item_data['upsells'] = array();
  
      // Append each upsells to product in cart
      foreach( $upsells as $upsell_id ) {
          $upsell_id = absint( $upsell_id );
  
          // Add extra parameter to the product which contain the upsells
          $woocommerce-&gt;cart-&gt;cart_contents[ $cart_item_key ]['upsells'] = $upsell_id;
  
          // Add upsell to cart
          $woocommerce-&gt;cart-&gt;add_to_cart( $upsell_id );
      }
  }
  add_action( 'woocommerce_add_to_cart', 'add_upsells_to_cart' );
  </code></pre>
  
  <p>Upsells are added to the cart as expected, but the extra row seems to be dropped by something in WC.</p>
  
  <p>When I do a <code>print_r( $woocommerce-&gt;cart-&gt;cart_contents[ $cart_item_key ] );</code> at the end of the previous function, the extra upsells parameter is visible, but when I display the cart from the cart page, the upsells parameter is dropped from the cart.</p>
  
  <p>Any idea?</p>
  
  <p>Thanks,
  E.</p>