3

Currently I am using WooCommerce for WordPress and trying to add custom fields for Variations Product. After did some researches, I found some code and tried to modify it.

This is my full code : https://gist.github.com/alphadc/da163cc95cfd1cede34a

add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
add_action( 'woocommerce_process_product_meta_variable-subscription' , 'save_variable_fields' , 10 , 1 ) ;

function variable_fields( $loop, $variation_data ) {?>
<tr>
<td>
  <?php
  woocommerce_wp_textarea_input( 
    array( 
      'id'          => '_weightdesc['.$loop.']', 
      'label'       => __( 'Weight Description', 'woocommerce' ), 
      'placeholder' => '', 
      'description' => __( 'Enter the custom value here.', 'woocommerce' ),
      'value'       => $variation_data['_weightdesc'][0],
    )
  );
  ?>
</td>
 </tr>
<?php }
function variable_fields_js()?>
<tr>
<td>
  <?php
  woocommerce_wp_textarea_input( 
    array( 
      'id'          => '_weightdesc[ + loop + ]', 
      'label'       => __( 'My Textarea', 'woocommerce' ), 
      'placeholder' => '', 
      'description' => __( 'Enter the custom value here.', 'woocommerce' ),
      'value'       => $variation_data['_weightdesc'][0],
    )
  );
  ?>
   </td>
  </tr>
<?php }
function save_variable_fields( $post_id ) {
  if (isset( $_POST['variable_sku'] ) ) :

$variable_sku          = $_POST['variable_sku'];
$variable_post_id      = $_POST['variable_post_id'];

// Textarea
$_weightdesc = $_POST['_weightdesc'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  $variation_id = (int) $variable_post_id[$i];
  if ( isset( $_weightdesc[$i] ) ) {
    update_post_meta( $variation_id, '_weightdesc', stripslashes( $_weightdesc[$i] ) );
  }
endfor;


endif;
}

The field is showing on my backend, but when I tried to save the value, it's not working. I tried to modified it too, but still not working.

I found this code from several sources one of this came from : http://www.remicorson.com/woocommerce-custom-fields-for-variations/#comment-14159

I think this is because of the update of WooCommerce (I am using 2.3.5).

Can someone help me please?

2
  • Please add all integral parts (stuff like code and images) directly to your question. Don't use third party services for that. Thank you :-) Mar 9, 2015 at 12:13
  • @PieterGoosen - I apologise for that. I just updated it. Can you please help me? :-)
    – Irwan
    Mar 9, 2015 at 12:31

3 Answers 3

5

Okay, based on answers on link above (where I got the old code and there are people that help to answered), I put the modification code for my website. I tried it and it's working like charm.

Change:

add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );

Into :

add_action( 'woocommerce_variation_options', 'variable_fields', 10, 3 );

And change :

'value'       => $variation_data['_weightdesc'][0],

Into :

'value' => get_post_meta($variation->ID, '_weightdesc', true)
1
  • you are life saviour!!
    – rAjA
    Apr 2, 2015 at 18:12
4

I know this post is old, but to keep this question updated:

As of WooCommerce 2.4.4

woocommerce_process_product_meta_variable no longer works, and it must be changed to woocommerce_save_product_variation

So,

Change:

add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );

Into:

add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );
1
  • Shouldn't woocommerce_save_product_variation take 2 parameters, as in: add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 ); Aug 17, 2015 at 19:46
-1
function variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input(
        array(
            'id'          => '_text_field[' . $variation->ID . ']',
            'label'       => __( 'My Text Field', 'woocommerce' ),
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );

}
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if ( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }
}

add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.