为什么有条件地删除 WooCommerce 计费字段不起作用?
Why is this conditional removal of WooCommerce billing field not working?
我的 functions.php 文件中有以下代码,用于检查某些产品是否在购物车中,如果在,则在结帐时从帐单字段集中删除一个字段。但是,它并没有删除该字段,也没有删除我尝试的任何其他字段。我想知道为什么会这样。我遵循的其他教程与此代码一致,并说它应该可以工作,但对我来说不是。我做错了什么?
/**
* Check if a specific product ID is in the cart
*/
function dz_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( '10771', '10773', '10774', '10943', '10944', '10945', '10946', '10947', '10948', '10949', '10950', '10951', '10952', '10953', '10943', '10943', '10943', '10943');;
// 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;
}
}
/**
* Conditionally remove a checkout field based on products in the cart
*/
function dz_remove_checkout_field( $fields ) {
if ( dz_product_is_in_the_cart() ) {
unset( $fields['billing']['billing_first_name'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'dz_remove_checkout_field' );
billing_first_name
字段是必填字段。那么在我尝试 unset
之前我是否需要做一些事情来取消它?
问题是插入此字段的插件覆盖了我试图取消要求并删除它的代码。我选择禁用插件并使用此处的代码添加和有条件地控制我自己的字段:https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7
我的 functions.php 文件中有以下代码,用于检查某些产品是否在购物车中,如果在,则在结帐时从帐单字段集中删除一个字段。但是,它并没有删除该字段,也没有删除我尝试的任何其他字段。我想知道为什么会这样。我遵循的其他教程与此代码一致,并说它应该可以工作,但对我来说不是。我做错了什么?
/**
* Check if a specific product ID is in the cart
*/
function dz_product_is_in_the_cart() {
// Add your special product IDs here
$ids = array( '10771', '10773', '10774', '10943', '10944', '10945', '10946', '10947', '10948', '10949', '10950', '10951', '10952', '10953', '10943', '10943', '10943', '10943');;
// 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;
}
}
/**
* Conditionally remove a checkout field based on products in the cart
*/
function dz_remove_checkout_field( $fields ) {
if ( dz_product_is_in_the_cart() ) {
unset( $fields['billing']['billing_first_name'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'dz_remove_checkout_field' );
billing_first_name
字段是必填字段。那么在我尝试 unset
之前我是否需要做一些事情来取消它?
问题是插入此字段的插件覆盖了我试图取消要求并删除它的代码。我选择禁用插件并使用此处的代码添加和有条件地控制我自己的字段:https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7