The "Did you need a bag" expression may be idiomatic in some locations, or possibly a particular turn of phrase used by staff in supermarkets.
However, it is not the best English in most circumstances. Your understanding and explanation are completely correct.
Better options would include the following.
- "Do you need a bag?"
- "Would you like a bag?"
The "Do you need" is correct but could come across as a bit abrupt (depending on the tone of voice), while the "Would you like" is more courteous.
Your second example is subtly different, as there are three scenarios.
Scenario 1: "Did you want to go to the cafe?" is generally a question about what you would have preferred to do in the past. For instance, while walking in a park, if your friend wonders why you look so grumpy she could ask: "Did you want to go to the cafe instead of being hungry and cold outdoors? You should have said so!".
Scenario 2: If your friend is asking about the future, then "Do you want to go to the cafe?" is normally the right phrase.
Scenario 3: There are, however, circumstances where your friend's original phase is perfectly acceptable. This is where the "did" applies to the past but the cafe visit is in the future.
We can see this most clearly if we expand the sentence to say: "Did you tell me this morning that you want to go to the cafe for lunch today?".
Even without the additional words, the "Did you want to go to the cafe?" is understood in that way - e.g. to mean "Were you planning to go ..." or "Were you thinking of the cafe rather than the pub?"