在 WooCommerce 结账表单中,我已经自定义了一些字段。但是,如果产品属于特定的类别,则不想显示这些字段。我已经实现了这部分内容。
接下来,我想取消一些自定义结账字段,但是我无法弄清楚如何做到这一点。取消普通的结账字段很容易,例如我使用以下代码:
由于这属于自己的部分,而不是账单、运送或常规的WooCommerce额外字段,我找不到删除它的方法。
我已经尝试了以下操作:
如何在WooCommerce中取消自定义结帐字段(不是经典字段)?
接下来,我想取消一些自定义结账字段,但是我无法弄清楚如何做到这一点。取消普通的结账字段很容易,例如我使用以下代码:
unset( $fields['billing']['billing_company'] );
然而对于自定义的结账字段,它无法正常工作。以下是我设置此自定义字段的方式:
function company_details_section($checkout)
{
woocommerce_form_field('delegate_1_name', array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide delegateExists'
) ,
'label' => __('Full name') ,
) , $checkout->get_value('delegate_1_name'));
}
由于这属于自己的部分,而不是账单、运送或常规的WooCommerce额外字段,我找不到删除它的方法。
我已经尝试了以下操作:
unset( $fields['delegate_1_name'] );
unset( $fields['additional']['delegate_1_name'] );
unset( $fields['billing']['delegate_1_name'] );
unset( $fields['shipping']['delegate_1_name'] );
unset( $fields['company_details_section']['delegate_1_name'] );
这是我正在使用的条件函数来取消设置字段:
function wc_ninja_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( '45', '70', '75' );;
// Products currently in the cart
$cart_ids = array();
// Find each product in the cart and add it to the $cart_ids array
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = $values['data'];
$cart_ids[] = $cart_product->id;
}
// If one of the special products are in the cart, return true.
if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
return true;
} else {
return false;
}
}
以下是取消普通结账字段的功能代码:
function wc_ninja_remove_checkout_field( $fields ) {
if ( ! wc_ninja_product_is_in_the_cart() ) {
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );
如何在WooCommerce中取消自定义结帐字段(不是经典字段)?