Reputation: 1
The WooCommerce shop that I am working on needs to have its Cart page update after the quantity is changed. Also, it can't have an Update Cart button as the one present by default. Any ideas on how do it, possibly with AJAX or any other clues?
Any help would be fantastic! Currently our page's cart is similar to http://www.shoopclothing.com and it should be like http://www.shoescribe.com/ for example.
What I've tried so far:
tried searching for the Update cart method itself but unable to find what exactly was changing the Cart subtotals.
also tried using AJAX to print out the page again without reloading it. That just reloaded the full page but the quantity was still the same
This is the script we've tried:
$(".product-order").on("click", "span.add", function () {
var form = $(this).closest('form').serialize();
$.ajax({
type: "POST",
url: "update_cart url",
data: form
}).done(function( msg ) {
console.log(msg);
alert("Data Saved: " + msg);
});
});
Upvotes: 0
Views: 5430
Reputation: 63
Thanks to Niko. I made little change to his code snippet. It now should works well even with ajax of woocommerce. And the cart will be updated automatically after user click "+" or "-".
$('body').on('click','table.cart .quantity',function(){
var itemQtyInitial;
jQuery('.woocommerce table.cart tr.cart_item .product-quantity').hover(function() {
itemQtyInitial = jQuery('.qty', this).val();
console.log("inital",itemQtyInitial);
}, function() {
var itemQtyExit = jQuery('.qty', this).val();
console.log("change",itemQtyExit);
if(itemQtyInitial != itemQtyExit) {
jQuery(".button[name='update_cart']").trigger("click");
}
});
});
Upvotes: 1
Reputation: 11
Hey if it's not too late, here is a quick solution that will update your cart without the user having to click the "Update Cart" button. It's not AJAX but it's a simple way to get it done.
Basically, if the user changes the quantity of any item, the cart will update once the user has exited the product-quantity td. It uses the .trigger() jQuery event handler to submit the form automatically.
Just use this jQuery code:
jQuery(document).ready(function() {
var itemQtyInitial;
jQuery('.woocommerce table.cart tr.cart_item .product-quantity').hover(function() {
itemQtyInitial = jQuery('.qty', this).val();
}, function() {
var itemQtyExit = jQuery('.qty', this).val();
if(itemQtyInitial != itemQtyExit) {
jQuery(".button[name='update_cart']").trigger("click");
}
});
});
Then you can hide the "Update Cart" button with this CSS:
.woocommerce table.cart td.actions input[name="update_cart"] {
display: none;
}
That should work!
Upvotes: 1
Reputation: 23
Your problem is that you send SERIALIZED data to update cart function, so it doesn't recognize post data, you have to put in your jquery post all the input fields
Upvotes: 0