Tickets can be purchased in person at the train station or online.
If purchasing online, only use the official site.
https://www.cp.pt/passageiros/en/buy-tickets
Tickets can be purchased a maximum of 60 days before departure.
Early purchase discount is often available sometimes it's the same or better than seniors discount.
Seniors discount is available, proof of age required.
Usually tickets can be changed for another train ( difference in price has to be paid) up to 15 minutes before departure, otherwise there's no recourse, you'll have to buy a new ticket.
Best to give yourself extra time between flight and train.