Hide conditionally Cart page coupon field in Woocommerce

Solution:

Try the following simplified code with some conditions checks to avoid this error problem. Also when watching for product categories in cart items always use $cart_item['product_id'] as this way it will work for product variations too.

add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_dinner_dances' );
function hide_coupon_field_dinner_dances( $enabled ){ 
    $cart = WC()->cart; // The WC_Cart Object
    
    // Only on cart page
    if( is_cart() && $cart && method_exists( $cart, 'get_cart' ) ) {
        $category = array('discount'); // <= Here define the product categories
        $enabled  = false; // Only enable when this product category is in cart
        
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            if ( has_term( $category, 'product_cat', $cart_item['product_id'] ) )  {
                $enabled = true;
                break;
            }
        }
    }
    return $enabled;
}

This should better works now avoiding the issue.