Ask first. That is the rule, and it simplifies most situations.
Morocco is a predominantly Muslim country and many people — particularly older women and those at prayer — consider being photographed without consent a genuine violation. In the souks, this sensitivity is layered with commerce: merchants who have been photographed by hundreds of tourists without acknowledgment tend to be reactive when they see another camera, because they have learned that tourists treat the medina as a film set rather than a community.
The practical approach is to slow down. Spend time at a stall, show genuine interest in what someone makes, and let the camera be secondary. "Photo, yes?" said with a gesture and a smile works in most cases. A refusal is a refusal — move on without argument. In Fes, many tannery balcony sellers run a formal pay-to-shoot arrangement (usually 10–20 MAD, indicative), which is entirely reasonable given how heavily their workplace is visited; treat it as an access fee rather than an imposition.
Children are a particular grey area. Many Moroccan families are uncomfortable with their children being photographed by strangers, especially for commercial or widely-shared images. If a child approaches you and poses, that is usually permission. Photographing children without the awareness of a nearby adult is not.