Is it grammatically correct to put a comma before "then"?
The word "then" can function as several parts of speech. For comma usage, the most important usage is as a conjunction. In theory, "then" is not a coordinating conjunction (like "and", "or"), but sometimes it is used as such.
Example: I went to bed, then I started dreaming.
Here, "then" is used as a shortened form of "and then". Some people might strongly disagree with this sort of sentence, but the truth is that it is widely used, even by The New York Times.
You can read more in this article.
We recommend that where "then" can be replaced by "and then" you use the same rules for comma placement as adding a comma before an "and", i.e. add one if the following phrase is an independent clause.