ajax coupon form. Closes #1061.

This commit is contained in:
Mike Jolley 2012-06-10 12:40:07 +01:00
parent 1443e07122
commit c6d6fd7901
10 changed files with 80 additions and 35 deletions

View File

@ -611,7 +611,7 @@ jQuery(document).ready(function($) {
update_checkout();
});
$('p.password, form.login, form.checkout_coupon, div.shipping_address').hide();
$('p.password, form.login, .checkout_coupon, div.shipping_address').hide();
$('input.show_password').change(function(){
$('p.password').slideToggle();
@ -623,7 +623,7 @@ jQuery(document).ready(function($) {
});
$('a.showcoupon').click(function(){
$('form.checkout_coupon').slideToggle();
$('.checkout_coupon').slideToggle();
return false;
});
@ -671,6 +671,40 @@ jQuery(document).ready(function($) {
// Update on page load
if (woocommerce_params.is_checkout==1) $('body').trigger('update_checkout');
/* AJAX Coupon Form Submission */
$('form.checkout_coupon').submit( function() {
var $form = $(this);
if ( $form.is('.processing') ) return false;
$form.addClass('processing').block({message: null, overlayCSS: {background: '#fff url(' + woocommerce_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
var data = {
action: 'woocommerce_apply_coupon',
security: woocommerce_params.apply_coupon_nonce,
coupon_code: $form.find('input[name=coupon_code]').val()
};
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: data,
success: function( code ) {
$('.woocommerce_error, .woocommerce_message').remove();
$form.removeClass('processing').unblock();
if ( code ) {
$form.before( code );
$form.slideUp();
$('body').trigger('update_checkout');
}
},
dataType: "html"
});
return false;
});
/* AJAX Form Submission */
$('form.checkout').submit( function() {
var $form = $(this);

File diff suppressed because one or more lines are too long

View File

@ -89,9 +89,8 @@ class WC_Checkout {
do_action('woocommerce_before_checkout_process');
if (sizeof($woocommerce->cart->get_cart())==0) :
if ( sizeof( $woocommerce->cart->get_cart() ) == 0 )
$woocommerce->add_error( sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>', 'woocommerce'), home_url()) );
endif;
do_action('woocommerce_checkout_process');

View File

@ -153,6 +153,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Feature - Mijireh Checkout Integration
* Feature - Menu count for orders requiring admin action
* Feature - 'supports' function for gateways.
* Feature - Ajax powered coupon form on checkout.
* Tweak - Improvements to the order tracking code, including better error messages
* Tweak - EU states for tax
* Tweak - woocommerce_shipping_chosen_method hook

View File

@ -21,13 +21,22 @@ function woocommerce_cart( $atts ) {
if ( ! defined( 'WOOCOMMERCE_CART' ) ) define( 'WOOCOMMERCE_CART', true );
// Add Discount
if ( ! empty( $_POST['apply_coupon'] ) ) {
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( stripslashes( trim( $_POST['coupon_code'] ) ) );
} else {
$woocommerce->add_error( __('Please enter a coupon code.', 'woocommerce') );
}
// Remove Discount Codes
if (isset($_GET['remove_discounts'])) :
} elseif ( isset( $_GET['remove_discounts'] ) ) {
$woocommerce->cart->remove_coupons( $_GET['remove_discounts'] );
// Update Shipping
elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && $woocommerce->verify_nonce('cart')) :
} elseif ( ! empty( $_POST['calc_shipping'] ) && $woocommerce->verify_nonce('cart') ) {
$validation = $woocommerce->validation();
@ -59,7 +68,7 @@ function woocommerce_cart( $atts ) {
endif;
endif;
}
// Calc totals
$woocommerce->cart->calculate_totals();

View File

@ -112,9 +112,10 @@ global $woocommerce;
</div>
<?php } ?>
<?php $woocommerce->nonce_field('cart') ?>
<input type="submit" class="button" name="update_cart" value="<?php _e('Update Cart', 'woocommerce'); ?>" /> <a href="<?php echo esc_url( $woocommerce->cart->get_checkout_url() ); ?>" class="checkout-button button alt"><?php _e('Proceed to Checkout &rarr;', 'woocommerce'); ?></a>
<?php do_action('woocommerce_proceed_to_checkout'); ?>
<?php $woocommerce->nonce_field('cart') ?>
</td>
</tr>

View File

@ -69,6 +69,28 @@ function woocommerce_sidebar_login_ajax_process() {
die();
}
/**
* AJAX apply coupon on checkout page
*/
add_action('wp_ajax_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon');
add_action('wp_ajax_nopriv_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon');
function woocommerce_ajax_apply_coupon() {
global $woocommerce;
check_ajax_referer( 'apply-coupon', 'security' );
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( stripslashes( trim( $_POST['coupon_code'] ) ) );
} else {
$woocommerce->add_error( __('Please enter a coupon code.', 'woocommerce') );
}
$woocommerce->show_messages();
die();
}
/**
* AJAX update shipping method on cart page
*/

View File

@ -247,6 +247,7 @@ function woocommerce_update_cart_action() {
$woocommerce->add_message( __('Cart updated.', 'woocommerce') );
$referer = ( wp_get_referer() ) ? wp_get_referer() : $woocommerce->cart->get_cart_url();
$referer = remove_query_arg( 'remove_discounts', $referer );
wp_safe_redirect( $referer );
exit;
@ -587,28 +588,6 @@ function woocommerce_process_login() {
endif;
}
/**
* Process the coupon form on the checkout and cart
**/
function woocommerce_process_coupon_form() {
global $woocommerce;
// Do nothing if coupons are globally disabled
if ( get_option( 'woocommerce_enable_coupons' ) == 'no' ) return;
if (isset($_POST['coupon_code']) && $_POST['coupon_code']) :
$coupon_code = stripslashes(trim($_POST['coupon_code']));
$woocommerce->cart->add_discount($coupon_code);
if ( wp_get_referer() ) :
wp_safe_redirect( remove_query_arg('remove_discounts', wp_get_referer()) );
exit;
endif;
endif;
}
/**
* Process the registration form
**/

View File

@ -130,7 +130,6 @@ add_action( 'init', 'woocommerce_pay_action', 10 );
/* Login and Registration */
add_action( 'init', 'woocommerce_process_login' );
add_action( 'init', 'woocommerce_process_coupon_form' );
add_action( 'init', 'woocommerce_process_registration' );
/* Product Downloads */

View File

@ -1013,6 +1013,7 @@ class Woocommerce {
'add_to_cart_nonce' => wp_create_nonce("add-to-cart"),
'update_order_review_nonce' => wp_create_nonce("update-order-review"),
'update_shipping_method_nonce' => wp_create_nonce("update-shipping-method"),
'apply_coupon_nonce' => wp_create_nonce("apply-coupon"),
'option_guest_checkout' => get_option('woocommerce_enable_guest_checkout'),
'checkout_url' => add_query_arg( 'action', 'woocommerce-checkout', $this->ajax_url() ),
'option_ajax_add_to_cart' => get_option('woocommerce_enable_ajax_add_to_cart'),