Skip to content
Advertisement

Calculate and update product price in cart – WooCommerce

I’m creating a WooCommerce (Version 5.6.0) site where users can buy foam products and the prices are calculated on the archive page. Basically, the users enter the dimensions and the price is calculated based on a formula. I’m currently struggling with updating the prices in cart, I don’t have previous experience in customizing the products and prices at this level, and so any help will be much appreciated.. Steps I’ve done so far:

1.Get (via Ajax) and set the custom product price in WC_Session

JavaScript

2.Change cart item price from WC_Session data:

JavaScript

The issue I’m running into, is that it’s working for the latest product added to cart only, the previous products added to cart are being re-set the 0 price every time I add a new product to cart. If anyone is able to provide some guidance, I will highly appreciate it. Thanks a mill!

Advertisement

Answer

With

JavaScript

you’re basically overriding the previous “foam_price” with the latest added to cart.

You need to store, in my opinion, different values for different product IDs.

Try with (untested):

JavaScript

…and then:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement