我需要根据产品类别添加自定义结账字段。 我找到了这段代码,可以与产品配合使用,但是每次添加特定类别的新产品的ID非常耗时。
function is_in_cart() {
// Add your special product IDs here
$ids = array( 12468, 9687, 9693);
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
if( in_array( $cart_item['data']->get_id(), $ids ) )
return true;
}
return false;
}
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_before_order_notes', 'custom_checkout_field', 20 );
function custom_checkout_field( $checkout ) {
if( ! is_in_cart() ){
woocommerce_form_field( 'special_field', array(
'type' => 'textarea',
'class' => array('form-row-wide'),
'label' => pll__('Special text'),
'required' => false,
'label_class' => array('specialfieldclass'),
'clear' => false,
), $checkout->get_value( 'special_field' ));
}
}
如何修改此代码以适配产品类别?