Have your say in Community Polls: What was/is your greatest motivation to start your own business?

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

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

loubacc
Tourist
6 0 2

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?

Reply 1 (1)

tobebuilds
Shopify Partner
478 34 129

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)

Founder, Regios Discounts app (4.9 stars, 68 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer