I mainly remember my parents doing these for my younger brother, but I'm assuming they did this for all three of us kids when they wanted to involve a kid in something and let them know it was okay to speak out. There are two ways of doing it, and which one you take depends on how shy your grandchild is.
If she's really shy, split up the approach at first. You address the stranger in the shop:
We appreciate the compliments, but she doesn't really like having her hair touched.
State this as a matter of fact, because you're standing up for her. After that, work on the boundaries when arriving home with your grandchild, by asking her if she did like having her hair touched, and assuring her it's okay to speak up just like you did if she did mind having her hair touched.
After a while, when you feel she's got a good enough grasp of this, you can even involve her in speaking up to the stranger, by asking a specific question addressed to her:
As much as we appreciate the compliments, she looked pretty uncomfortable there. I don't think she liked having her hair touched. Did you like having your hair touched, Tink?
Here, you give your grandchild the 'task' of making this a statement of fact. That way, she learns to make the statement 'I don't like having my hair touched' herself. Make sure to give the language that follows up on 'I don't think she liked' a tweak that is appropriate for the level of understanding a four-year-old has.
Take care though: your granddaughter might've looked uncomfortable because she actually was, or because she's a bit overwhelmed with the stranger in general. If it's the second, she might also feel too shy to answer the direct question when the stranger is still present. In that case, let her be when she's too shy and revert back to working on this at home. Don't force her to answer, but make sure to give her praise (perhaps not when the stranger can hear) when she does!
I'm guessing you know her best, but as a warning to every other custodian thinking of using this approach: be prepared for a child to say 'no' to a specific question like 'did you not like having your hair touched', for a reason you totally don't expect. If she's overwhelmed with the stranger, she might not be able to say it, but she might say it wasn't because he touched her hair.
The responses to the strangers that my parents used also lead by example by showing assertiveness (and a bit of politeness), which is great for enforcing boundaries: You're respecting the good intent from the stranger, while also letting them know they've just crossed a (possible) boundary. While you're basically saying the stranger did something wrong, you're avoiding being accusatory by using words like "we", "I", "she", instead of directly addressing the stranger with a "you did".
There's a lot more stuff you can do to teach your child to enforce boundaries, most of them are going to be unrelated to the situation you described above though, and are probably better done when she's slightly older. Some of the stuff like described here that may be suitable for showing a younger child they have a right to boundaries:
- respecting when they say no in reply to a request for a kiss or hug,
- respecting their feelings,
- encourage them to show their feelings and
- praising them when they show feelings in an acceptable manner.
Again, I assume you know your granddaughter best, and can best determine if/when she's ready for any of the other steps the article describes. There's a lot of great looking online sources that turn up on a quick google search, they describe all sorts of stuff you could do to help her develop healthy boundaries along the way.