title: Adding affiliate links to Woocommerce variations
body: <p>I am trying to add affiliate links to the Woocommerce variations. The idea is to have a unique http link/URL(affiliate link) for each product variation. A link/URL that I can enter in the Woocommerce backend and when a customer clicks on the 'Add to cart' button, a new web-page is loaded (based on the corresponding URL of that product)</p>
  
  <p>This can be easily achieved for the products with no variations. But there is no such out of the box functionality for achieving the same thing for the products with variations.</p>
  
  <p>I came across a solution over <a href="https://designloud.com/how-to-add-affiliate-links-to-woocommerce-variations/" rel="nofollow noreferrer">here</a> 
  I implemented the code but whenever I try to enter the URL in the backend and try to save my changes, the link disappears, don't know what's going wrong with my code.</p>
  
  <pre><code>    // Display Fields
  add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
  //JS to add fields for new variations
  add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
  // Save Fields
  add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
  
  function variable_fields( $loop, $variation_data ) {
  ?&gt;  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;div&gt;
                      &lt;label&gt;&lt;?php _e( 'Affiliate URL', 'woocommerce' ); ?&gt;&lt;/label&gt;
                      &lt;input type="text" size="5" name="my_affiliate_url[&lt;?php echo $loop; ?&gt;]" value="&lt;?php echo $variation_data['_my_affiliate_url'][0]; ?&gt;"/&gt;
  
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;?php
  }
  
  function variable_fields_js() {
  ?&gt;
  &lt;tr&gt;
          &lt;td&gt;
              &lt;div&gt;
                      &lt;label&gt;&lt;?php _e( 'My Custom Field', 'woocommerce' ); ?&gt;&lt;/label&gt;
                      &lt;input type="text" size="5" name="my_affiliate_url[' + loop + ']" /&gt;
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;?php
  }
  
  function variable_fields_process( $post_id ) {
      if (isset( $_POST['variable_sku'] ) ) :
          $variable_sku = $_POST['variable_sku'];
          $variable_post_id = $_POST['variable_post_id'];
          $variable_custom_field = $_POST['my_affiliate_url'];
          for ( $i = 0; $i &lt; sizeof( $variable_sku ); $i++ ) :
              $variation_id = (int) $variable_post_id[$i];
              if ( isset( $variable_custom_field[$i] ) ) {
                  update_post_meta( $variation_id, '_my_affiliate_url', stripslashes( $variable_custom_field[$i] ) );
              }
          endfor;
      endif;
  }
  
  //front-end variations
  function woocommerce_variable_add_to_cart() {
          global $product, $post;
          $variations = $product-&gt;get_available_variations();
          foreach ($variations as $key =&gt; $value) {
          ?&gt;
          &lt;form action="&lt;?php echo esc_url( $product-&gt;add_to_cart_url() ); ?&gt;"method="post" enctype='multipart/form-data'&gt;
              &lt;input type="hidden" name="variation_id" value="&lt;?php echo $value['variation_id']?&gt;" /&gt;
              &lt;input type="hidden" name="product_id" value="&lt;?php echo esc_attr( $post-&gt;ID ); ?&gt;" /&gt;
              &lt;?php
              if(!empty($value['attributes'])){
                  foreach ($value['attributes'] as $attr_key =&gt; $attr_value) {
                  ?&gt;
                  &lt;input type="hidden" name="&lt;?php echo $attr_key?&gt;" value="&lt;?php echo $attr_value?&gt;"&gt;
                  &lt;?php
                  }
              }
              ?&gt;
              &lt;table&gt;
                  &lt;tbody&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;b&gt;&lt;?php echo implode('/', $value['attributes']);?&gt;&lt;/b&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;?php echo $value['price_html'];?&gt;
                          &lt;/td&gt;
                          &lt;td&gt;
                              &lt;a class="single_add_to_cart_button button alt" target="_blank" href="&lt;?php echo get_post_meta($value['variation_id'], '_my_affiliate_url', true); ?&gt;" &gt;&lt;?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product-&gt;product_type); ?&gt;&lt;/a&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/tbody&gt;
              &lt;/table&gt;
          &lt;/form&gt;
          &lt;?php
          }
  }
  </code></pre>