They aren’t allowed to contractually. The PBMs and insurance companies make pharmacies sign contracts that forbid them from doing this. They also forbid pharmacies from telling customers if it would be cheaper to pay cash.
Most states have only a few major insurance companies and getting caught and losing the ability to serve a large portion of clients would put most pharmacies out of business.