skip to Main Content

I sell event tickets and accept donations on https://development.pittsburghconcertsociety.org. When someone buys a ticket, they must agree to the covid policies. But when someone only "buys" a donation, i.e. they put only a donation product in the cart, they do not need to agree to covid policies. The woo support chatbot spit out this code, but it’s not working:

function hide_terms_for_specific_product( $woocommerce_checkout_fields ) {
    // Check if the specific product is the only item in the cart
    if ( WC()->cart ) {
        $cart_items = WC()->cart->get_cart();
        $specific_product_found = false;

        foreach ( $cart_items as $cart_item ) {
            // Replace '123' with the ID of the specific product
            if ( $cart_item['product_id'] == 551 ) {
                $specific_product_found = true;
                break;
            }
        }

        // Hide terms and conditions for the specific product
        if ( $specific_product_found ) {
            unset( $woocommerce_checkout_fields['terms'] );
        }
    }

    return $woocommerce_checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'hide_terms_for_specific_product' );

(Donation is product ID 551). To summarize, I do want the T&C checkbox/requirement if a donation is in the cart along with a ticket, but not if a donation is the only item in the cart. And it’s not enough to just hide the T&C in this case; it also needs to not be required.

Gravy: be able to add more than one product ID in case we sell merch.

2

Answers


  1. You were not using the correct hook. The following will remove T&C entirely when a specific product is alone in 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 = 123;
        $cart_items = WC()->cart->get_cart(); // get cart items
    
        // Check if there is only one item in cart
        if( count($cart_items) > 1 ) {
            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;
    }
    

    Code goes in functions.php file of the active child theme (or in a plugin). Tested and works.

    Login or Signup to reply.
  2. In case it is helpful I created a similar script which enables TOS on products in a specific category only. Also had to add a check for WC()->cart as this sometimes caused a fatal error if null.

    add_filter('woocommerce_checkout_show_terms', 'remove_terms_and_conditions_checkbox_for_specific_category');
    
    function remove_terms_and_conditions_checkbox_for_specific_category($show_terms) {
        // Check if WooCommerce is active and the cart object is available
        if (in_array('woocommerce/woocommerce.php', get_option('active_plugins')) && WC()->cart) {
            // Define the product category slug to check for
            $targeted_category_slug = 'your-product-cat-slug-here';
    
            // Get the cart contents
            $cart_items = WC()->cart->get_cart();
    
            $has_specific_category = false;
    
            // Check if any cart item belongs to the specific category
            foreach ($cart_items as $cart_item) {
                $product = wc_get_product($cart_item['product_id']);
                $product_categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'slugs'));
    
                if (in_array($targeted_category_slug, $product_categories)) {
                    $has_specific_category = true;
                    break;
                }
            }
    
            // If a product from the specific category is in the cart, show terms & conditions checkbox
            if ($has_specific_category) {
                return $show_terms;
            }
    
            // If no product from the specific category is in the cart, hide terms and conditions checkbox
            return false;
        }
    
        // If WooCommerce is not active or the cart object doesn't exist, hide terms and conditions checkbox
        return false;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search