Forum
Shop cart update prices with JS
Is it possible to update the total prices within the cart via JavaScript? I am calculating hire prices for a product based on dates and am currently passing the results through to with the order using cart-property="Hire Dates"
etc but was wandering if it is possible to update the actual prices associated to the order? If so how?
Many thanks!
Yes, you'd need to create an endpoint in your site that outputs the data you need and then request that from your JavaScript.
Could you elaborate on this or point me towards some examples?
I don't have any specific examples of this. Which part are you struggling with?
I dont know what you mean by 'create an endpoint in your site that outputs the data'. I have a normal cart setup which has the item data. How can I update the prices within the cart to reflect prices calculated via JS?
Currently in cart.html I have:
The
<td>
is being passed a price via JS but I'm not sure how I then pass this price into<perch:cartitem id="price" />
to update the order data.Hi Rob,
the endpoint would usually be a php page - the sole job of this page would be to call php functions to update the cart - not outputting html or anything.
There is a shop function
perch_shop_add_to_cart()
(link to docs) which also allows you provide a quantity like so:Within your javascript code, you could send data to your endpoint like so:
The php page grabs those params and passes them to the function.
I don't think there is a function to change product prices, but for your example, working out the number of days and using that as a quantity of a day 'product' with associated price may suffice?
Thank you Duncan, that explains it brilliantly. Much appreciated!