'Deny checkout for specific cart items in Woocommerce based on product categories

Based on Allow checkout only when a product of a mandatory category is in cart I tried to make my own code sample that Renders a notice and prevents checkout if the cart only contains products in specific categories. It works on prevention and error notice however on adding other products, it still denies checkout.

/**
 * Renders a notice and prevents checkout if the cart
 * only contains products in a specific category
 */
//add_action( 'woocommerce_check_cart_items', 'brown_wc_prevent_checkout_for_category' );

function brown_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $categories = array('drinks','extra-accompaniments');

    foreach ($categories as $category => $value) {
        // get the product category
        $product_cat = get_term_by( 'slug', $category, 'product_cat' );

        // sanity check to prevent fatals if the term doesn't exist
        if ( is_wp_error( $product_cat ) ) {
            return;
        }

        // check if this category is the only thing in the cart
        if ( brown_wc_is_category_alone_in_cart( $category ) ) {

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( 'Please choose atleast one bento.', $category ), 'error' );
        }
    }

}

/**
 * Checks if a cart contains exclusively products in a given category
 * 
 * @param string $category the slug of the product category
 * @return bool - true if the cart only contains the given category
 */
function brown_wc_is_category_alone_in_cart( $category ) {

    //When the cart is empty, remove warning message that I have set for when the snippet takes effect
    if (!WC()->cart->is_empty()) {
        // All the code
        // check each cart item for our category
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            // if a product is not in our category, bail out since we know the category is not alone
            if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
                return false;
            }
        }

        // if we're here, all items in the cart are in our category
        return true;

    } else {

        return false;   //  Assume you'd want false here, since the cart is empty

    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source