In the synoptics this event was recorded near the end of Jesus’ days, and led almost immediately to his Passion. It seems that John felt little need for maintaining chronological sequence in his gospel.
Looking at the Bible as a whole it seems clear that what happened here is a continuation or repetition of things that had happened throughout the history of the Hebrew nation. They were constantly slipping back into horrid practices and constantly called to account by the prophets.
What we have is a continuous conflict between prophet and priest. The priest is always allied with the temporal power, and the prophet always attempts to bring the people back to their spiritual calling. As Jesus said, they stoned the prophets and built monuments afterward; the temple of his day was such a monument. Moslems are supposed to honor Jesus as a prophet, which indicates more insight than we customarily show.
We’re still doing it of course. We need another prophet to denounce the Church for confirming our warlike stance.