Variant price changed dynamically

echilviet
Visitor
2 0 0

I have a product with variants, and have specified the price for each variant.  However, depending of "options" selected by users I'd like to adjust the price of the variant selected. I manage to change "variant.price" on the client side, but not on the server side. So, when the item is added to the cart, the old/original price remains.
Question: How can I change the price of existing variants (by either javascript of liquid code)? If I need to use API, could I have some sample code? Thanks.

Replies 4 (4)

diego_ezfy
Shopify Partner
2935 562 883

I'm not entirely sure what you mean. When the user picks a new variant does the price visually change on your website?

If not, you'll need to listen for changes on that specific variant <select> tag using Javascript. Once there's a change, then you update the price.

◦ Follow my blog & youtube for coding tutorials. Most questions in here are already answered there!
◦ Top #4 Shopify Expert, 24h reply. Click here to hire me.
Download copy/paste code snippets that can replace most apps.

echilviet
Visitor
2 0 0

Imagine that I'm selling a car (product), and its variants are the various "brands" I offer. I can easily define each variant with its own price.  However, if for each variant (brand) I offer add-ons (e.g. sound system options, navigation option, sun-roof, etc.), I don't want to "pre-build" each possible combination as a separate variant with its own price.  What I'd like to have is once a user specifies a variant (brand), which has a unique (base) price, I'd like to change the price of that variant based on the add-on options the buyer chooses for that variant.  I can pass on the selected add-ons as properties when added to the cart--no problem.  But, I haven't been able to modify the price of the variant based on the add-ons selected by the buyer. I'm able to change the price displayed when a buyer is choosing add-ons, but I cannot pass the updated price to the cart. Hopefully this makes sense.  I appreciate your help.

jaxter
Shopify Partner
27 1 9

Hi echilviet,

Charles001
Excursionist
26 0 2

Hi @Hardik29418 

Where abouts should i past the code above in order to allow the product price to change with selections of variants eg. size

 

Thanks,