我搜索如何在woocommerce中为可验证产品添加自定义字段。我已经这样做了,但只适用于简单的产品。
add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_custom_text_field',
'label' => 'Custom Text Field',
'description' => 'This is a custom field, you can write here anything you want.',
'desc_tip' => 'true',
'placeholder' => 'Custom text'
) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
如何编辑它以获得变量产品?
答案 0 :(得分:0)
需要执行该操作
add_action( 'woocommerce_product_after_variable_attributes', 'wc_custom_add_custom_fields', 10, 3 );
答案 1 :(得分:-1)
只需更改
add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
到
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );
,您的自定义字段将位于广告资源标签
中