Cart AJAX: Add update call.

Since the code is built to use a form submit for all the quanitities
already, I just made this an ajax call instead of a whole page call.
The result is the exactly the same and the .woocommerce div is replaced
with the resulting HTML.
This commit is contained in:
Kevin Killingsworth 2016-02-14 21:57:14 -06:00
parent e99e75b5b9
commit 9a43b897da
2 changed files with 28 additions and 1 deletions

View File

@ -145,4 +145,31 @@ jQuery( function( $ ) {
}
});
});
// Quantity Update
$( document ).on( 'click', '[name=update_cart]', function( evt ) {
evt.preventDefault();
var $form = $( 'div.woocommerce > form' );
// Provide the submit button value because wc-form-handler expects it.
$( '<input />').attr( 'type', 'hidden' )
.attr( 'name', 'update_cart' )
.attr( 'value', 'Update Cart' )
.appendTo( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
// Grab html response and replace the new .woocommerce div.
var $html = $.parseHTML( response );
var $new_div = $( 'div.woocommerce', $html );
$( 'div.woocommerce').replaceWith( $new_div );
}
});
});
});

View File

@ -1 +1 @@
jQuery(function(a){var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")},d=function(a){a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()};if("undefined"==typeof wc_cart_params)return!1;a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var c=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){c[a(this).data("index")]=a(this).val()}),d(a("div.cart_totals"));var e={security:wc_cart_params.update_shipping_method_nonce,shipping_method:c};a.post(b("update_shipping_method"),e,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),a(".shipping-calculator-form").hide();var f=function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(b){a("div.cart_totals").replaceWith(b)}})},g=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)};a('[name="apply_coupon"]').on("click",function(h){h.preventDefault();var i=a("div.woocommerce > form");if(c(i))return!1;d(i);var j=a("#coupon_code"),k=j.val(),l={security:wc_cart_params.apply_coupon_nonce,coupon_code:k};a.ajax({type:"POST",url:b("apply_coupon"),data:l,dataType:"html",success:function(a){g(a)},complete:function(){e(i),j.val(""),f()}})}),a(document).on("click","a.woocommerce-remove-coupon",function(c){c.preventDefault();var h=a(this).parents("tr"),i=a(this).attr("data-coupon");d(h.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:i};a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(a){g(a),e(h.parents("table"))},complete:function(){f()}})})});
jQuery(function(a){var b=function(a){return wc_cart_params.wc_ajax_url.toString().replace("%%endpoint%%",a)},c=function(a){return a.is(".processing")},d=function(a){a.addClass("processing").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},e=function(a){a.removeClass("processing").unblock()};if("undefined"==typeof wc_cart_params)return!1;a(document).on("click",".shipping-calculator-button",function(){return a(".shipping-calculator-form").slideToggle("slow"),!1}).on("change","select.shipping_method, input[name^=shipping_method]",function(){var c=[];a("select.shipping_method, input[name^=shipping_method][type=radio]:checked, input[name^=shipping_method][type=hidden]").each(function(){c[a(this).data("index")]=a(this).val()}),d(a("div.cart_totals"));var e={security:wc_cart_params.update_shipping_method_nonce,shipping_method:c};a.post(b("update_shipping_method"),e,function(b){a("div.cart_totals").replaceWith(b),a(document.body).trigger("updated_shipping_method")})}),a(".shipping-calculator-form").hide();var f=function(){d(a("div.cart_totals")),a.ajax({url:b("get_cart_totals"),dataType:"html",success:function(b){a("div.cart_totals").replaceWith(b)}})},g=function(b){var c=a("div.woocommerce > form");a(".woocommerce-error, .woocommerce-message").remove(),c.before(b)};a('[name="apply_coupon"]').on("click",function(h){h.preventDefault();var i=a("div.woocommerce > form");if(c(i))return!1;d(i);var j=a("#coupon_code"),k=j.val(),l={security:wc_cart_params.apply_coupon_nonce,coupon_code:k};a.ajax({type:"POST",url:b("apply_coupon"),data:l,dataType:"html",success:function(a){g(a)},complete:function(){e(i),j.val(""),f()}})}),a(document).on("click","a.woocommerce-remove-coupon",function(c){c.preventDefault();var h=a(this).parents("tr"),i=a(this).attr("data-coupon");d(h.parents("table"));var j={security:wc_cart_params.remove_coupon_nonce,coupon:i};a.ajax({type:"POST",url:b("remove_coupon"),data:j,dataType:"html",success:function(a){g(a),e(h.parents("table"))},complete:function(){f()}})}),a(document).on("click","[name=update_cart]",function(b){b.preventDefault();var c=a("div.woocommerce > form");a("<input />").attr("type","hidden").attr("name","update_cart").attr("value","Update Cart").appendTo(c),a.ajax({type:c.attr("method"),url:c.attr("action"),data:c.serialize(),dataType:"html",success:function(b){var c=a.parseHTML(b),d=a("div.woocommerce",c);a("div.woocommerce").replaceWith(d)}})})});