From 7c321a22691cf81c8e9be9d47b0bdea1541f9b88 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 13 Jul 2017 12:15:13 +0100 Subject: [PATCH] Ajaxify widget remove from cart button Fixes #13171 --- assets/js/frontend/add-to-cart.js | 31 ++++++++++++++++++++++++++- assets/js/frontend/add-to-cart.min.js | 2 +- includes/class-wc-ajax.php | 16 ++++++++++++++ templates/cart/mini-cart.php | 5 +++-- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/assets/js/frontend/add-to-cart.js b/assets/js/frontend/add-to-cart.js index 78631bace7a..c95b74a0288 100644 --- a/assets/js/frontend/add-to-cart.js +++ b/assets/js/frontend/add-to-cart.js @@ -11,9 +11,10 @@ jQuery( function( $ ) { var AddToCartHandler = function() { $( document ) .on( 'click', '.add_to_cart_button', this.onAddToCart ) + .on( 'click', '.remove_from_cart_button', this.onRemoveFromCart ) .on( 'added_to_cart', this.updateButton ) .on( 'added_to_cart', this.updateCartPage ) - .on( 'added_to_cart', this.updateFragments ); + .on( 'added_to_cart removed_from_cart', this.updateFragments ); }; /** @@ -64,6 +65,34 @@ jQuery( function( $ ) { } }; + /** + * Update fragments after remove from cart event in mini-cart. + */ + AddToCartHandler.prototype.onRemoveFromCart = function( e ) { + var $thisbutton = $( this ), + $row = $thisbutton.closest( '.woocommerce-mini-cart-item' ); + + e.preventDefault(); + + $row.block({ + message: null, + overlayCSS: { + opacity: 0.6 + } + }); + + $.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), { cart_item_key : $thisbutton.data( 'cart_item_key' ) }, function( response ) { + if ( ! response || ! response.fragments ) { + window.location = $thisbutton.attr( 'href' ); + return; + } + $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash ] ); + }).fail( function() { + window.location = $thisbutton.attr( 'href' ); + return; + }); + }; + /** * Update cart page elements after add to cart events. */ diff --git a/assets/js/frontend/add-to-cart.min.js b/assets/js/frontend/add-to-cart.min.js index 61ae52b2866..17d88a7a39c 100644 --- a/assets/js/frontend/add-to-cart.min.js +++ b/assets/js/frontend/add-to-cart.min.js @@ -1 +1 @@ -jQuery(function(t){if("undefined"==typeof wc_add_to_cart_params)return!1;var a=function(){t(document).on("click",".add_to_cart_button",this.onAddToCart).on("added_to_cart",this.updateButton).on("added_to_cart",this.updateCartPage).on("added_to_cart",this.updateFragments)};a.prototype.onAddToCart=function(a){var d=t(this);if(d.is(".ajax_add_to_cart")){if(!d.attr("data-product_id"))return!0;a.preventDefault(),d.removeClass("added"),d.addClass("loading");var o={};t.each(d.data(),function(t,a){o[t]=a}),t(document.body).trigger("adding_to_cart",[d,o]),t.post(wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","add_to_cart"),o,function(a){a&&(a.error&&a.product_url?window.location=a.product_url:"yes"!==wc_add_to_cart_params.cart_redirect_after_add?t(document.body).trigger("added_to_cart",[a.fragments,a.cart_hash,d]):window.location=wc_add_to_cart_params.cart_url)})}},a.prototype.updateButton=function(a,d,o,r){(r=void 0!==r&&r)&&(r.removeClass("loading"),r.addClass("added"),wc_add_to_cart_params.is_cart||0!==r.parent().find(".added_to_cart").length||r.after(' '+wc_add_to_cart_params.i18n_view_cart+""),t(document.body).trigger("wc_cart_button_updated",[r]))},a.prototype.updateCartPage=function(){var a=window.location.toString().replace("add-to-cart","added-to-cart");t(".shop_table.cart").load(a+" .shop_table.cart:eq(0) > *",function(){t(".shop_table.cart").stop(!0).css("opacity","1").unblock(),t(document.body).trigger("cart_page_refreshed")}),t(".cart_totals").load(a+" .cart_totals:eq(0) > *",function(){t(".cart_totals").stop(!0).css("opacity","1").unblock(),t(document.body).trigger("cart_totals_refreshed")})},a.prototype.updateFragments=function(a,d){d&&(t.each(d,function(a){t(a).addClass("updating").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}})}),t.each(d,function(a,d){t(a).replaceWith(d),t(a).stop(!0).css("opacity","1").unblock()}),t(document.body).trigger("wc_fragments_loaded"))},new a}); \ No newline at end of file +jQuery(function(t){if("undefined"==typeof wc_add_to_cart_params)return!1;var a=function(){t(document).on("click",".add_to_cart_button",this.onAddToCart).on("click",".remove_from_cart_button",this.onRemoveFromCart).on("added_to_cart",this.updateButton).on("added_to_cart",this.updateCartPage).on("added_to_cart removed_from_cart",this.updateFragments)};a.prototype.onAddToCart=function(a){var o=t(this);if(o.is(".ajax_add_to_cart")){if(!o.attr("data-product_id"))return!0;a.preventDefault(),o.removeClass("added"),o.addClass("loading");var r={};t.each(o.data(),function(t,a){r[t]=a}),t(document.body).trigger("adding_to_cart",[o,r]),t.post(wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","add_to_cart"),r,function(a){a&&(a.error&&a.product_url?window.location=a.product_url:"yes"!==wc_add_to_cart_params.cart_redirect_after_add?t(document.body).trigger("added_to_cart",[a.fragments,a.cart_hash,o]):window.location=wc_add_to_cart_params.cart_url)})}},a.prototype.onRemoveFromCart=function(a){var o=t(this),r=o.closest(".woocommerce-mini-cart-item");a.preventDefault(),r.block({message:null,overlayCSS:{opacity:.6}}),t.post(wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","remove_from_cart"),{cart_item_key:o.data("cart_item_key")},function(a){a&&a.fragments?t(document.body).trigger("removed_from_cart",[a.fragments,a.cart_hash]):window.location=o.attr("href")}).fail(function(){window.location=o.attr("href")})},a.prototype.updateButton=function(a,o,r,e){(e=void 0!==e&&e)&&(e.removeClass("loading"),e.addClass("added"),wc_add_to_cart_params.is_cart||0!==e.parent().find(".added_to_cart").length||e.after(' '+wc_add_to_cart_params.i18n_view_cart+""),t(document.body).trigger("wc_cart_button_updated",[e]))},a.prototype.updateCartPage=function(){var a=window.location.toString().replace("add-to-cart","added-to-cart");t(".shop_table.cart").load(a+" .shop_table.cart:eq(0) > *",function(){t(".shop_table.cart").stop(!0).css("opacity","1").unblock(),t(document.body).trigger("cart_page_refreshed")}),t(".cart_totals").load(a+" .cart_totals:eq(0) > *",function(){t(".cart_totals").stop(!0).css("opacity","1").unblock(),t(document.body).trigger("cart_totals_refreshed")})},a.prototype.updateFragments=function(a,o){o&&(t.each(o,function(a){t(a).addClass("updating").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}})}),t.each(o,function(a,o){t(a).replaceWith(o),t(a).stop(!0).css("opacity","1").unblock()}),t(document.body).trigger("wc_fragments_loaded"))},new a}); \ No newline at end of file diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 09493a2a4de..0cd9dcf522a 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -93,6 +93,7 @@ class WC_AJAX { 'get_cart_totals' => true, 'update_order_review' => true, 'add_to_cart' => true, + 'remove_from_cart' => true, 'checkout' => true, 'get_variation' => true, 'get_customer_location' => true, @@ -382,6 +383,21 @@ class WC_AJAX { } } + /** + * AJAX add to cart. + */ + public static function remove_from_cart() { + ob_start(); + + $cart_item_key = wc_clean( $_POST['cart_item_key'] ); + + if ( $cart_item_key && false !== WC()->cart->remove_cart_item( $cart_item_key ) ) { + self::get_refreshed_fragments(); + } else { + wp_send_json_error(); + } + } + /** * Process ajax checkout form. */ diff --git a/templates/cart/mini-cart.php b/templates/cart/mini-cart.php index f1dd6e1a4a7..f7679dee0cc 100644 --- a/templates/cart/mini-cart.php +++ b/templates/cart/mini-cart.php @@ -15,7 +15,7 @@ * @see https://docs.woocommerce.com/document/template-structure/ * @author WooThemes * @package WooCommerce/Templates - * @version 3.1.0 + * @version 3.2.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; @@ -42,10 +42,11 @@ do_action( 'woocommerce_before_mini_cart' ); ?>
  • ×', + '×', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'woocommerce' ), esc_attr( $product_id ), + esc_attr( $cart_item_key ), esc_attr( $_product->get_sku() ) ), $cart_item_key ); ?>