Liquid, JavaScript, themes, sales channels
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.
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.
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.
Hi echilviet,
I think you can't change the price of the variant on the server side. There may be an API call that allows this to happen, but that would change the price of that variant for everyone, and I don't think that is what you really want. I think what you want is a dynamic price calculation and to put your calculated price onto the line item. I think that is not really allowed by Shopify.
There are some apps, like BCPO, I think, where you can add options as properties that have 'add-on' prices that build up on the base variant price. It doesn't change the variant price, but it does add to the total in the cart. I don't know exactly how it does it.
I'm not an expert, but I have done a lot of research trying to do some dynamic pricing without finding any method to do it directly. I think 'Shopify Scripts' may allow something, but it's only availallbe to Shopify Plus accounts, so not for most people.
Regards, Jack
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,
Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024Digital marketers and app developers have tracked activity in apps and websites for yea...
By Ollie Mar 13, 2024February was an exciting month with Shopify Editions, informative webinars, and more! F...
By JasonH Mar 7, 2024