It's almost always that I'm behind them at the checkout/cashier.
If they're struggling, card declined etc, they're probably already frustrated, they might turn to me to apologise, that's my cue; I'll just say, "hey, can I get that for you?“. I remember the first time, a guy about my age just buying food, his card was declined, he had a little bit of cash so the cashier was having him figure out what to leave behind so he had enough to cover it. I just asked if I could get it all for him and he just asked, "why?", looking puzzled, I just said "why not?", and something along the lines of "you can pass on the favour one day", he accepted.
Other times its that the place doesn't take cards and they need to get cash, I'll just say, they can add it to my bill and I'll get it, if they don't mind.
Sometimes people decline, but usually I'm pretty good at judging when people would appreciate it so it's rarely awkward.