我在以下方面需要您的帮助:在Woocommerce的结帐页面上,我需要检查是否添加了特色产品。如果是,则从结帐表单中删除一些其他字段。
我已经尝试了以下代码,但是没有用:
add_filter( 'woocommerce_checkout_fields' , 'wc_featured_product_add_checkout_fields' );
function wc_featured_product_add_checkout_fields( $fields ) {
global $product;
if ( $product->is_featured() ) {
unset($fields['billing']['billing_family_name']);
}
return $fields;
}
谢谢你的帮助。
如果产品具有特色,请从结帐表单中删除一些其他字段。在代码中添加说明的注释
function wc_featured_product_add_checkout_fields( $fields ) {
// Set variable
$found = false;
// Loop trough cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object
$product = $cart_item['data'];
// Is featured
if ( $product->is_featured() ) {
$found = true;
break;
}
}
// True
if ( $found ) {
// Unset
unset( $fields['billing']['billing_first_name'] );
// Etc..
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_featured_product_add_checkout_fields', 10, 1 );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句