ajaxified coupon removal on checkout page for better UX closes #6881

This commit is contained in:
splashingpixels 2014-12-07 22:15:03 -08:00
parent 72f41440dd
commit 25b6377298
5 changed files with 71 additions and 4 deletions

View File

@ -401,6 +401,47 @@ jQuery( function( $ ) {
return false;
});
$( '.woocommerce-checkout-review-order' ).on( 'click', '.woocommerce-remove-coupon', function( e ) {
e.preventDefault();
var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
coupon = $( this ).data( 'coupon' );
container.addClass( 'processing' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
action: 'woocommerce_remove_coupon',
security: wc_checkout_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: data,
success: function( code ) {
$( '.woocommerce-error, .woocommerce-message' ).remove();
container.removeClass( 'processing' ).unblock();
if ( code ) {
container.before( code );
$( 'body' ).trigger( 'update_checkout' );
// remove coupon code from coupon field
$( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
}
},
dataType: 'html'
});
});
$( 'body' )
// Init trigger

File diff suppressed because one or more lines are too long

View File

@ -26,6 +26,7 @@ class WC_AJAX {
$ajax_events = array(
'get_refreshed_fragments' => true,
'apply_coupon' => true,
'remove_coupon' => true,
'update_shipping_method' => true,
'update_order_review' => true,
'add_to_cart' => true,
@ -118,6 +119,30 @@ class WC_AJAX {
die();
}
/**
* AJAX remove coupon on cart and checkout page
*/
public static function remove_coupon() {
check_ajax_referer( 'remove-coupon', 'security' );
$coupon = wc_clean( $_POST['coupon'] );
if ( ! isset( $coupon ) || empty( $coupon ) ) {
wc_add_notice( __( 'Sorry there was a problem removing this coupon.', 'woocommerce' ) );
} else {
WC()->cart->remove_coupon( $coupon );
wc_add_notice( __( 'Coupon has been removed.', 'woocommerce' ) );
}
wc_print_notices();
die();
}
/**
* AJAX update shipping method on cart page
*/

View File

@ -202,8 +202,9 @@ class WC_Frontend_Scripts {
case 'wc-checkout' :
return array(
'ajax_url' => WC()->ajax_url(),
'update_order_review_nonce' => wp_create_nonce( "update-order-review" ),
'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ),
'update_order_review_nonce' => wp_create_nonce( 'update-order-review' ),
'apply_coupon_nonce' => wp_create_nonce( 'apply-coupon' ),
'remove_coupon_nonce' => wp_create_nonce( 'remove-coupon' ),
'option_guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
'checkout_url' => add_query_arg( 'action', 'woocommerce_checkout', WC()->ajax_url() ),
'is_checkout' => is_page( wc_get_page_id( 'checkout' ) ) && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) ? 1 : 0

View File

@ -219,7 +219,7 @@ function wc_cart_totals_coupon_html( $coupon ) {
// get rid of empty array elements
$value = array_filter( $value );
$value = implode( ', ', $value ) . ' <a href="' . add_query_arg( 'remove_coupon', $coupon->code, defined( 'WOOCOMMERCE_CHECKOUT' ) ? WC()->cart->get_checkout_url() : WC()->cart->get_cart_url() ) . '" class="woocommerce-remove-coupon">' . __( '[Remove]', 'woocommerce' ) . '</a>';
$value = implode( ', ', $value ) . ' <a href="' . add_query_arg( 'remove_coupon', $coupon->code, defined( 'WOOCOMMERCE_CHECKOUT' ) ? WC()->cart->get_checkout_url() : WC()->cart->get_cart_url() ) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr( $coupon->code ) . '">' . __( '[Remove]', 'woocommerce' ) . '</a>';
echo apply_filters( 'woocommerce_cart_totals_coupon_html', $value, $coupon );
}