That exception is triggered in a claim of a violation of the Establishment Clause and when there is a specific legislative grant for spending that implicates the First Amendment.
He agreed with the four conservatives that the law violated the Commerce Clause of the Constitution, which was the main claim of the 26 states that sued.
Lochner, which was effectively reversed in a series of post-New Deal decisions, did not involve a federal law--contrary to the president's claim--and thus had nothing to do with the Commerce Clause, which concerns only the powers of Congress.