Check coupons are valid before cart/checkout
This commit is contained in:
parent
ff82bb3a26
commit
967da77d88
|
@ -51,6 +51,7 @@ class WC_Cart {
|
|||
$this->get_cart_from_session();
|
||||
|
||||
add_action('woocommerce_check_cart_items', array(&$this, 'check_cart_items'), 1);
|
||||
add_action('woocommerce_check_cart_items', array(&$this, 'check_cart_coupons'), 1);
|
||||
add_action('woocommerce_after_checkout_validation', array(&$this, 'check_customer_coupons'), 1);
|
||||
}
|
||||
|
||||
|
@ -231,6 +232,29 @@ class WC_Cart {
|
|||
$woocommerce->add_error( $result->get_error_message() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check cart coupons for errors
|
||||
*/
|
||||
function check_cart_coupons() {
|
||||
global $woocommerce;
|
||||
|
||||
if ( ! empty( $this->applied_coupons ) ) {
|
||||
foreach ( $this->applied_coupons as $key => $code ) {
|
||||
$coupon = new WC_Coupon( $code );
|
||||
|
||||
if ( is_wp_error( $coupon->is_valid() ) ) {
|
||||
|
||||
$woocommerce->add_error( sprintf( __('Sorry, it seems the coupon "%s" is invalid - it has now been removed from your order.', 'woocommerce'), $code ) );
|
||||
|
||||
// Remove the coupon
|
||||
unset( $this->applied_coupons[$key] );
|
||||
$_SESSION['coupons'] = $this->applied_coupons;
|
||||
$_SESSION['refresh_totals'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart items quantities - merged so we can do accurate stock checks
|
||||
*
|
||||
|
|
|
@ -215,7 +215,8 @@ class WC_Coupon {
|
|||
|
||||
$valid = apply_filters( 'woocommerce_coupon_is_valid', $valid, $this );
|
||||
|
||||
if ( $valid ) return true;
|
||||
if ( $valid )
|
||||
return true;
|
||||
|
||||
endif;
|
||||
|
||||
|
|
|
@ -944,7 +944,7 @@ class WC_Product {
|
|||
/** Depreciated - naming was confusing */
|
||||
function get_available_attribute_variations() {
|
||||
_deprecated_function( 'get_available_attribute_variations', '1.5.7', 'get_variation_attributes' );
|
||||
$this->get_variation_attributes();
|
||||
return $this->get_variation_attributes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -193,6 +193,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
|
|||
* Tweak - Exempt from VAT shows price ex VAT
|
||||
* Tweak - Newlines for order meta (flat)
|
||||
* Tweak - Show method title when free
|
||||
* Fix - check_cart_coupons on checkout to prevent invalid coupons being used
|
||||
* Fix - Hide out of stock now works with widgets.
|
||||
* Fix - Strange error where detecting the page (is_page etc) would break the canonical redirect in some instances when hooked into 'wp'. Used the later get_header hook instead.
|
||||
* Fix - Right now links.
|
||||
|
|
|
@ -70,11 +70,12 @@ function woocommerce_cart( $atts ) {
|
|||
|
||||
}
|
||||
|
||||
// Check cart items are valid
|
||||
do_action('woocommerce_check_cart_items');
|
||||
|
||||
// Calc totals
|
||||
$woocommerce->cart->calculate_totals();
|
||||
|
||||
do_action('woocommerce_check_cart_items');
|
||||
|
||||
if (sizeof($woocommerce->cart->get_cart())==0) :
|
||||
|
||||
woocommerce_get_template( 'cart/empty.php' );
|
||||
|
|
Loading…
Reference in New Issue