您将需要挂钩:
woocommerce_process_product_meta_variable
所以像这样的东西可以解决问题:
add_action( 'woocommerce_process_product_meta_variable', 'new_custom_field_here', 10, 1 );
function new_custom_field_here( $loop, $variation_data ) { ?>
<label>New Field</label>
<input name="new_field_[<?php echo $loop; ?>]" value="<?php echo $variation_data['_new_field'][0]; ?>"/>
<?php }
这将创建一个新字段。然后,您需要保存这些数据:
add_action( 'woocommerce_process_product_meta_variable', 'save_new_custom_field', 10, 1 );
function save_new_custom_field( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$new_field = $_POST['new_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $new_field [$i] ) ) {
update_post_meta( $variation_id, '_new_field', stripslashes( $new_field [$i] ) );
update_post_meta( $variation_id, '_parent_product', $post_id );
}
endfor;
endif;
}
我希望这能让你走上正轨!