Skip to content

Instantly share code, notes, and snippets.

@whitsend79
Created December 13, 2013 03:53
Show Gist options
  • Save whitsend79/7939543 to your computer and use it in GitHub Desktop.
Save whitsend79/7939543 to your computer and use it in GitHub Desktop.
My product variations custom fields are not saving.
//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 variation fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'Manufacturer', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_manufacturer'][0]; ?>"/>
</div>
</td>
</tr>
<tr>
<td>
<div>
<label><?php _e( 'Part Number', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_part_no'][0]; ?>"/>
</div>
</td>
</tr>
<tr>
<td>
<div>
<label><?php _e( 'UPC Code', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_upc_code'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>\
<td>\
<div>\
<label><?php _e( 'Manufacturer', 'woocommerce' ); ?></label>\
<input type="text" size="5" name="my_custom_field[' + loop + ']" />\
</div>\
</td>\
</tr>\
<tr>\
<td>\
<div>\
<label><?php _e( 'Part Number', 'woocommerce' ); ?></label>\
<input type="text" size="5" name="my_custom_field[' + loop + ']" />\
</div>\
</td>\
</tr>\
<tr>\
<td>\
<div>\
<label><?php _e( 'UPC Code', 'woocommerce' ); ?></label>\
<input type="text" size="5" name="my_custom_field[' + loop + ']" />\
</div>\
</td>\
</tr>\
<?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['manufacturer'];
$variable_custom_field = $_POST['part_no'];
$variable_custom_field = $_POST['upc_code'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_manufacturer', stripslashes( $variable_custom_field[$i] ) );
update_post_meta( $variation_id, '_part_no', stripslashes( $variable_custom_field[$i] ) );
update_post_meta( $variation_id, '_upc_code', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment