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?