在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中的自定義結帳字段(不是經典的)?
在什麼時候適用於所有版本的WooCommerce你在他們解封? – inarilo
請參閱上面的編輯。 – RD2411
您是否嘗試打印$字段? – inarilo