1 回答
TA贡献1858条经验 获得超8个赞
更新 2 - 在这种情况下使用的正确钩子是woocommerce_checkout_init操作钩子:
// Custom conditional function that checks if there is only virtual items in cart
function has_only_virtual_items_in_cart(){
$only_virtual = true;
// Check if there are non-virtual items in cart
foreach( WC()->cart->get_cart() as $cart_item ) {
if ( ! $cart_item['data']->is_virtual() ) {
$only_virtual = false;
break;
}
}
return $only_virtual;
}
// Unhook some functions conditionally
add_action( 'woocommerce_checkout_init', 'simplify_checkout_for_virtual_items_only' );
function simplify_checkout_for_virtual_items_only() {
if( has_only_virtual_items_in_cart() ) {
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_checkout_privacy_policy_text', 20 );
remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 );
remove_action( 'woocommerce_checkout_terms_and_conditions', 'woocontracts_terms_fields' ); // <== missing priority
remove_action( 'woocommerce_checkout_after_terms_and_conditions', 'woocontracts_checkout_additional_checkboxes', 10 );
}
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
然后,您可以在挂钩中的函数has_only_virtual_items_in_cart()内重用自定义条件函数。woocontracts_checkout_field_process()woocommerce_checkout_process
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报