Skip to content

Commit

Permalink
Cart ajax: apply coupon
Browse files Browse the repository at this point in the history
Adds an ajax call for applying coupons while on the cart
screen (not checkout, as it already has this.)

This is the first commit to add ajax calls to the cart update
functions. See issue woocommerce#6734
  • Loading branch information
coderkevin committed Feb 12, 2016
1 parent 07db701 commit ce90c44
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
48 changes: 48 additions & 0 deletions assets/js/frontend/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,52 @@ jQuery( function( $ ) {
});

$( '.shipping-calculator-form' ).hide();

// Update the cart after something has changed.
var update_cart_totals = function() {
$( 'div.cart_totals' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});

var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_cart_totals' );

$.ajax( {
url: url,
success: function( response ) {
$( 'div.cart_totals' ).replaceWith( response );
}
});
};

// Coupon code
$( '[name="apply_coupon"]' ).on( 'click', function( evt ) {
evt.preventDefault();

var text_field = $( '#coupon_code' );
var coupon_code = text_field.val();
text_field.val( '' );

var url = wc_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' );
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};

$.ajax( {
type: 'POST',
url: url,
data: data,
success: function( response ) {
window.console.log( 'apply coupon response:' );
window.console.log( response );
},
complete: function() {
update_cart_totals();
}
});
});
});
2 changes: 1 addition & 1 deletion assets/js/frontend/cart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions includes/class-wc-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static function add_ajax_events() {
'apply_coupon' => true,
'remove_coupon' => true,
'update_shipping_method' => true,
'get_cart_totals' => true,
'update_order_review' => true,
'add_to_cart' => true,
'checkout' => true,
Expand Down Expand Up @@ -254,6 +255,22 @@ public static function update_shipping_method() {
die();
}

/**
* AJAX receive updated cart_totals div.
*/
public static function get_cart_totals() {

if ( !defined( 'WOOCOMMERCE_CART' ) ) {
define( 'WOOCOMMERCE_CART', true );
}

WC()->cart->calculate_totals();

woocommerce_cart_totals();

die();
}

/**
* AJAX update order review on checkout.
*/
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-frontend-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private static function get_script_data( $handle ) {
'ajax_url' => WC()->ajax_url(),
'wc_ajax_url' => WC_AJAX::get_endpoint( "%%endpoint%%" ),
'update_shipping_method_nonce' => wp_create_nonce( "update-shipping-method" ),
'apply_coupon_nonce' => wp_create_nonce( "apply-coupon" ),
);
break;
case 'wc-cart-fragments' :
Expand Down

0 comments on commit ce90c44

Please sign in to comment.