Purchasing magic items? Simple, aside from basic scrolls and potions, they don't just buy them, they commission them. The difference is subtle but significant, and has an impact on game play. Here's how I'd handle the circumstance you mention.
Firstly, I don't have a 'random city' - I can usually guess where the PCs are headed, so I can work up at least basic city profiles (as the DMG) in advance. This gives me some ideas about the Spellcasters resident in the city. I then work through these casters - or at least the more powerful ones - deciding their interests and if they are item crafters. Since I've in the past put plenty of legwork into looking at the mixtures of magical groups in my campaign world, I don't find this too difficult.
So now I have an idea of the likely capabilties of the item crafters in that city. Next I'll also think over if any of them are likely to be otherwise engaged at this time - that is engaged in research, crafting large projects for local potentates, etc. That gives me a profile of the sorts of items that are likely to be available, so when characters ask around I can indicate what local gossip says is available. I know it sounds like a lot of work, but really, unless it is a major metropolis the scope is actually fairly limited, and all this takes no more than an hour to work out.
Third, I think about the location of the city - what sort of lands surround it, what its neighbours are like, what special sites are around. This gives me some ideas for the likelihood of 'secondhand' magic items being available. At tis point I also sprinkle in some personal ideas for magic items, including ones that are likely to be beyond the means of characters to buy! (Well, it has aspiration value, and it adds to verisimilitude - it seems strange if only items appropriate for their level are circulating!).
When it comes to the PCs actually purchasing, this tends to be an adventure of sorts in its own right - I have even been known to award XP for handling the process well - which helps concetrate the minds of the players on the task! PCs take the opportunity to find out what is available, and who might supply it. Social PCs can spend some time just socialising, getting to know people who might help them. Then when they have an idea what they want they can approach the relevant crafter to negotiate the commission. Here's where they get an idea of how long it's going to be before they can actually take delivery - and financial inducements can improve this time! I can also plant adventure seeds since these sorts of people often also need favours doing - from dealing with troublesome competitors to delivering commissions to gathering special components. And the time taken for commissions automatically creates some downtime in a game - giving PC crafters a break.
All this makes buying magic items more than just 'looking stuff up in the DMG'. Reviewing what I've written, this makes the process seem laborious, but really I tend to think of it as all part of the lifelong adventures of the PCs. Approaching it like that makes the events more noteworthy and more interesting for all involved. Characters who want special items may need to persuade others to visit the right places, or do favours, etc. Overall, it gives me some control over the input of items into the setting, whilst not depriving the players of an important factor in the design of their characters.