This (long-standing) practice is of course rendered far easier if you have a magic whistle.
Pets increase their loyalty to you by eating food which you have handled, so even if you are short of money it is worth getting into a shop ahead of your pet (shutting it out if necessary) and picking up and dropping anything in there that the pet might eat.