With some airlines you can either specify the fare class (e.g. United's 'advanced search' mode), or the airline website offers you the options for semi-flexible/flexible or bundled fares where the fare classes for higher price fares are different to those of lower fares.
But where the airline website does not show the fare class when booking (e.g. TAP), or all fare options shown are in a non-earning class or classes (e.g. LH group), then one workaround is to search for flights on
matrix.itasoftware.com, where you can specify the fare class you want, then send the fare construction from ITA Matrix to a travel agent. It means booking through a TA, which can be slower if you are booking outside of the TA's office hours, and can bring downsides like an admin fee, having to make changes through the TA, or not getting full access to the airline's manage booking functionality( for some airlines), but it is a way to ensure your fare is booked in the fare class you want.
With Star Alliance codeshare bookings, it is the operating airline's fare class that governs the miles awarded, and sometimes the booked fare class maps to a different fare class on the operating airline. If I am booking a codeshare I try to book a class that is available for both the marketing and operating airline, and earns the required percentage of miles for both the operating airline and the marketing airline (check on
www.wheretocredit.com), as this seems to reduce the chances of discovering after flying that you have earned less points than you expected.