您將需要掛接到:
woocommerce_process_product_meta_variable
所以這樣的事情會做的伎倆:
add_action('woocommerce_process_product_meta_variable', 'function 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;
}
我希望這能讓您走上正軌!