我在产品订单表上添加了一个额外的字段,以便在订购特定产品时允许自定义注释:
// Display custom field on single product page
function d_extra_product_field(){
$value = isset( $_POST ['extra_product_field'] ) ? sanitize_text_field( $_POST['extra_product_field'] ) : '';
printf( '<div><label>%s</label><br><textarea name="extra_product_field" value="%s"></textarea></div>', __( 'Notes: Select colours & quantities for 12 pack only' ), esc_attr( $value ) );
}
add_action( 'woocommerce_after_add_to_cart_button', 'd_extra_product_field', 9 );
然而,它 for each 产品添加了该字段.
我希望它仅添加特定产品类别(类别ID 29)的自定义字段.
这是我的代码try :
// Display custom field on single product page
function d_extra_product_field(){
$value = isset( $_POST ['extra_product_field'], $product_cat_id = 29 ) ? sanitize_text_field( $_POST['extra_product_field'] ) : '';
printf( '<div><label>%s</label><br><textarea name="extra_product_field" value="%s"></textarea></div>', __( 'Notes: Select colours & quantities for 12 pack only' ), esc_attr( $value ) );
}
add_action( 'woocommerce_after_add_to_cart_button', 'd_extra_product_field', 9 );
但它不起作用.如何仅针对特定WooCommerce产品类别显示此额外产品字段?