Skip to content

Instantly share code, notes, and snippets.

@pauloiankoski
Created January 14, 2016 00:00
Show Gist options
  • Save pauloiankoski/78a94ae0c11ad15dac9c to your computer and use it in GitHub Desktop.
Save pauloiankoski/78a94ae0c11ad15dac9c to your computer and use it in GitHub Desktop.
WooCommerce Ajax Cart Update
jQuery(document).ready(function($){
var woocommerce_form = $( '.woocommerce-cart form' );
woocommerce_form.on('change', '.qty', function(){
form = $(this).closest('form');
// emulates button Update cart click
$("<input type='hidden' name='update_cart' id='update_cart' value='1'>").appendTo(form);
// get the form data before disable button...
formData = form.serialize();
// disable update cart and proceed to checkout buttons before send ajax request
$("input[name='update_cart']").val('Updating…').prop('disabled', true);
$("a.checkout-button.wc-forward").addClass('disabled').html('Updating…');
// update cart via ajax
$.post( form.attr('action'), formData, function(resp) {
// get updated data on response cart
var shop_table = $('table.shop_table.cart', resp).html();
var cart_totals = $('.cart-collaterals .cart_totals', resp).html();
// replace current data by updated data
$('.woocommerce-cart table.shop_table.cart')
.html(shop_table)
.find('.qty')
.before('<input type="button" value="-" class="minus">')
.after('<input type="button" value="+" class="plus">');
$('.woocommerce-cart .cart-collaterals .cart_totals').html(cart_totals);
});
}).on('click','.quantity input.minus', function() {
var current = $(this).next('.qty').val();
current--;
$(this).next('.qty').val(current).trigger('change');
}).on('click','.quantity input.plus', function() {
var current = $(this).prev('.qty').val();
current++;
$(this).prev('.qty').val(current).trigger('change');
})
$( '.woocommerce-cart' ).on( 'click', "a.checkout-button.wc-forward.disabled", function(e) {
e.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment