P粉3443557152023-07-31 00:43:10
The following code will completely remove the T&C requirement when only specific products are in the cart:
add_filter( 'woocommerce_checkout_show_terms', 'remove_terms_and_conditions_for_specific_unique_item' ); function remove_terms_and_conditions_for_specific_unique_item( $show_terms ) { // Replace "123" with the desired product ID $targeted_id = 15; $cart_items = WC()->cart->get_cart(); // get cart items // Check if there is only one item in cart if( count($cart_items) > 2 ) { return $show_terms; } // Check if the targeted product ID is the only item in cart if ( reset($cart_items)['product_id'] == $targeted_id ) { return false; // Remove terms and conditions field } return $show_terms; }
The code should be placed in the functions.php file of the active child theme, or placed in a plugin. Has been tested and confirmed to work.