How to automatically change total in woocommerce product page when quantity change?

View QuestionsCategory: CodeHow to automatically change total in woocommerce product page when quantity change?
admin Staff asked 4 years ago
1 Answers
admin Staff answered 4 years ago

Add this JS code on load

/* add auto quantity calc */
if(jQuery('body.single-product').length>0 && jQuery('form.cart span.woocommerce-Price-amount').length>0 && jQuery('form .tc-totals-form').length ==0){
/*check this is single product and TM extra option plugin is not on*/
/*avoid any conflict to other plugins that does similar things*/

//clone price element
jQuery('form.cart span.woocommerce-Price-amount').clone().appendTo('form.cart .price')

//hide first one
jQuery('form.cart span.woocommerce-Price-amount').first().hide();

//set trigger - update on change of quantity
jQuery('input.qty').change(function(){
var quan = parseFloat(jQuery('form.cart span.woocommerce-Price-amount').first().text().replace('$',''))
var price = parseInt(jQuery('input.qty').val())
var total = price * quan
var prettyTotal = Number(total).toFixed(2)
jQuery('form.cart span.woocommerce-Price-amount').eq(1).text('$' + prettyTotal)
})

}