I just learned that I should not say:
I could buy it yesterday.
but rather use “was able” or “could have”.
At the same time, I have just seen a sentence: He could help me but he did not. That is correct?
It depends on context if the subjunctive mood is required or not
If you had already purchased a product yesterday, and today it's not available, then, "I could buy it yesterday," is a statement of fact, and grammatically correct. It could also be written as "I could buy it yesterday and I did."
On the other hand, if you had seen the product yesterday but not bought it, and today it's not available, then, "I could have bought it yesterday," using the subjunctive mood is needed. That is, "I could have bought it yesterday and didn't, and now I cannot."
Maybe by some prescriptivist grammars, I could buy it yesterday doesn't work. It probably breaks some rules; lots of things do.
It is, however, absolutely normal in every dialect that I'm familiar with. It is generally equivalent to the more formally correct "I could have bought it yesterday".