【发布时间】:2023-03-19 15:46:01
【问题描述】:
我需要根据产品类别添加自定义结帐字段。我发现这段代码适用于产品,但是从特定类别的每个新产品中添加 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' ));
}
}
如何修改此代码以使用产品类别?
【问题讨论】:
标签: php wordpress woocommerce field checkout