WooCommerce: Calculate Subtotal On Quantity Increment @ Single Product
From a UX point of view, ecommerce customers may enjoy a little improvement on the WooCommerce single product page. As soon as they increase the add to cart quantity, it’d be nice if product price could be recalculated or maybe if a “TOTAL” line could appear so that users always know how much they are about to add to cart.
Honestly, this is hard to explain it this way, so the best is if you look at the screenshot. Enjoy!
Product price is $34 and quantity is 10, therefore the “Total” is now $340. Whenever quantity changes, “Total” updates its value.
PHP Snippet: Calculate Total Price Upon Quantity Increment @ WooCommerce Single Product Page
/**
* @snippet Calculate Subtotal Based on Quantity - WooCommerce Single Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 4.1
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_product_price_recalculate' );
function bbloomer_product_price_recalculate() {
global $product;
echo '<div id="subtot" style="display:inline-block;">Total: <span></span></div>';
$price = $product->get_price();
$currency = get_woocommerce_currency_symbol();
wc_enqueue_js( "
$('[name=quantity]').on('input change', function() {
var qty = $(this).val();
var price = '" . esc_js( $price ) . "';
var price_string = (price*qty).toFixed(2);
$('#subtot > span').html('" . esc_js( $currency ) . "'+price_string);
}).change();
" );
}
Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.
If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
37 thoughts on “WooCommerce: Calculate Subtotal On Quantity Increment @ Single Product”
Asad
Dear Rudolfo,
Thanks for your helpful Code. Is it possible to display the regular price along with subtotal as my product is on sale
would be very thankful if you help me out
Hi Asad thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
thanks for your helpful Code Snippets. Is it possible to show the subtotal price in another currency format? For example: 15,10 € . The subtotal price is 15 euros and 10 cent.
We would be very happy if you had an idea. Have an nice day 🙂
Best regards from Germany Lena
Hi Michael, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I have another query, suppose i have set of products I want to sell and by default I have set quantity as 4. I want the actual product price equal to the default quantity that is set, then later calculate it after the quantity is increased.
Hi Gaurang, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Calculate Subtotal Based on Quantity – WooCommerce Single Product not working for Variable products ..
As you can see simple product name – Salted pista Rs 599 Total price is changing on increasing the quantity, on vairable product i.e Dried dates, Its 299- 600 the price is as per the weight , so whenever i’m updating the quantity on different weights, the total amount is still default to the main cost Rs 299, its not changing with the weight price.
but i think this could be a problem on the grouped products too
Thanks
Hello Efrain, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Vlad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Great snippet, thanks! Rather than adding the div price subtotal output, I adapted the script to target the class of the existing WC product price so they update when the quantity changes. Worked perfectly 🙂
Hi Bob, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
I thank your blog, it’s super cool.
I using this code in my site, but de code not function it doesn’t work when I add the product via the ajax add card.
I think it is the JS that is not being read, can you tell me how I can solve this problem?
Hi Rodolfo: I saw this code implemented on a site I visited the other day and thought it was a nice feature to have. So, thanks for sharing this code with us as it is much appreciated! I have already added the code to my dev site.
-Dave
Hey Rodolfo, I agree – this will definitely improve UX. I’ll make sure to use it on my next WooCommerce project. I feel Woo should actually include this functionality in core. Maybe one day. 🙂
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Dear Rudolfo,
Thanks for your helpful Code. Is it possible to display the regular price along with subtotal as my product is on sale
would be very thankful if you help me out
Thanks
Hi Asad thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Dear Rudolfo,
thanks for your helpful Code Snippets. Is it possible to show the subtotal price in another currency format? For example: 15,10 € . The subtotal price is 15 euros and 10 cent.
We would be very happy if you had an idea. Have an nice day 🙂
Best regards from Germany Lena
Yes!
Hello and thank you so much
Please when Im trying to change the two lines in function I got an error could you please type the full code of this
Thank you again
not working
Please expand on that?
Hi,
Can it be modified to fit the yith dynamic pricing ajax discount?
To show the total price for each discounted variant quantity, instead of the unit price?
Hi Michael, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo.
This one worked out perfectly.
I have another query, suppose i have set of products I want to sell and by default I have set quantity as 4. I want the actual product price equal to the default quantity that is set, then later calculate it after the quantity is increased.
Thankyou
Hi Gaurang, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Calculate Subtotal Based on Quantity – WooCommerce Single Product not working for Variable products ..
As you can see simple product name – Salted pista Rs 599 Total price is changing on increasing the quantity, on vairable product i.e Dried dates, Its 299- 600 the price is as per the weight , so whenever i’m updating the quantity on different weights, the total amount is still default to the main cost Rs 299, its not changing with the weight price.
but i think this could be a problem on the grouped products too
Thanks
That’s possible, probably I only tested it on Simple products
this works, yet, it doesn’t account for the specific variation price, but rather the lowest price possible of the item.
https://prnt.sc/wl9257
Do you know how to adjust the code so it adds the specific variation price, and not just the lowest priced variation?
Hello Efrain, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
It’s working on my site but it’s not changing the total price when it’s a variable product.
I see, I guess this was coded for simple products only
Hi Rodolfo,
Please can you change the code for variables products?
Tks
It works, but does not conform to the currency settings of the site. In Europe decimals are separated with a comma and not a dot.
Yes, you’ll need to adjust the JS to print the format you want
Hello, is it possible to make it work with your custom product qty switchers?
https://prnt.sc/ukezhu
Hi Vlad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Great snippet, thanks! Rather than adding the div price subtotal output, I adapted the script to target the class of the existing WC product price so they update when the quantity changes. Worked perfectly 🙂
Nice! Feel free to share
This would be even better and more useful to the customer:
Ok thank you
I think the idea you have put forward is a great improvement but is there any way the code can be changed to work with variations.
Hi Bob, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello,
I thank your blog, it’s super cool.
I using this code in my site, but de code not function it doesn’t work when I add the product via the ajax add card.
I think it is the JS that is not being read, can you tell me how I can solve this problem?
Thank you
Hey Xavier, this is for the single product page, and there is no Ajax there
Hi Rodolfo: I saw this code implemented on a site I visited the other day and thought it was a nice feature to have. So, thanks for sharing this code with us as it is much appreciated! I have already added the code to my dev site.
-Dave
The only other enhancement I need to make to this code is for Variations. Shouldn’t be pretty easy and straight-forward to add the code.
Awesome!
Hi Dave! Did you figure out how to adapt it for product variations?
If you did, I’d love to know how you did it. I’m trying to make it work, but it doesn’t seem to be doing it.
Thanks Rodolfo, great code!
Hey Rodolfo, I agree – this will definitely improve UX. I’ll make sure to use it on my next WooCommerce project. I feel Woo should actually include this functionality in core. Maybe one day. 🙂
Great!