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!
PHP Snippet: Calculate Total Price Upon Quantity Increment @ WooCommerce Single Product Page
/**
* @snippet Calculate Subtotal Based on Quantity - WooCommerce Single Product
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 4.1
* @community https://businessbloomer.com/club/
*/
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();
" );
}
Hi 🙂
Thank you for all the tips! not just for this article!
I’ve adapted your code to mine, to take into account the currency converter plugin I have installed. However, upon page load, the JS script does not run, it only runs and total gets update only if I increase/decrease the qty. Can you please help and tell me what I’m doing wrong?
Thanks,
Hey Sabine, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Any chance you can make this work on archive page with quantity selectors?
Hello Majd, 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. 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!