How to redirect expiring posts to category page?

I have a set of posts with products that are going to become out of stock at some point and once that happens I would like to redirect these posts to their respective category pages.

I’ve looked through the docs and the forum but I couldn’t find something. Is there a way to do this?

P.S. As a workaround I thought about setting up a post like a custom 404 and redirect these posts with aliases but ideally I would prefer to point to the actual category page.

Should be possible.

You would:

  1. Have to build/deploy on interval.
  2. In template: Conditional: Redirect (Javascript or meta?) if stock eq 0 or something.