Price in add to cart removes when selecting a different variant.

Hello guys. I have implemented custom code from this thread: click here. This added the price in the Add to cart button but removes when I select a different variant on the product page.

Does anyone know how I can fix this?

You’ll need to modify that code to add an “event listener” that overwrites the “Add to cart” button’s innerHTML again each time a new variant is selected.

Every theme is different, so detecting when a new variant is selected could be a challenge. However, many themes’ product pages have a hidden input (named id) that always stores the numerical ID of the currently selected variant. You could listen to its change event and use the AJAX API to get the price for the currently selected variant.

(You can also use Liquid to serialize the prices of all variants as JSON to the page during rendering, so that you don’t have to call the AJAX API each time)