In America I can walk in to a restaurant, even a nicer one and ask to use the bathroom and very likely would be shown strait to it. It’s not 100% of the time everywhere, especially places like Times Square, but I’d say that the norm is no spend required.
If you're a well groomed adult and walk up to the counter at a coffee shop and ask for the code, they'll usually just give it to you no questions asked.
The selective enforcement is the point, because they use these policies to keep away the "types" of people they don't want in the businesses (i.e. unhoused people, teens, etc).
Obviously can't speak for the entire country of Spain, but that tends to be true here too. The more touristic places, especially closer to the beaches, tend to have "Toilets for customers only" signs though, which is kind of understandable as people start using them as changing rooms otherwise, and get overrun with tourists not really caring about how they leave things.
Even then, I cannot imagine somebody saying no when telling them "I have a health issue and need to use a toilet", in most cases.