The correct option is D I was going to buy it
The main clause in the given sentence is in simple past tense and implies a condition/action that has already taken place.
However, the subordinate clause of the sentence indicates an uncompleted action of the past. It indicates something that should have happened but did not.
Thus, option C "I was going to buy it" follows the Subject + was + verb (+ ing) + object. Thus, this is the correct answer.
"I would buy it" in option A implies a wish or preference. This does not concur with the context.
"I will have bought it" uses the incorrect tense.
"I'll buy it" is not in tense agreement with the main clause of the sentence.
Thus, option C is the correct answer.