Better errors. Closes #1018.

This commit is contained in:
Mike Jolley 2012-06-10 19:07:19 +01:00
parent 4d7d184698
commit ec4a59432a
3 changed files with 25 additions and 12 deletions

View File

@ -408,7 +408,7 @@ class WC_Cart {
} }
if ($flat) if ($flat)
$return .= implode( ", \n",, $variation_list ); $return .= implode( ", \n", $variation_list );
else else
$return .= implode( '', $variation_list ); $return .= implode( '', $variation_list );
@ -1537,8 +1537,9 @@ class WC_Cart {
if ( $the_coupon->id ) { if ( $the_coupon->id ) {
// Check it can be used with cart // Check it can be used with cart
if ( ! $the_coupon->is_valid() ) { $return = $the_coupon->is_valid();
$woocommerce->add_error( __('Invalid coupon.', 'woocommerce') ); if ( ! $return || is_wp_error( $return ) ) {
$woocommerce->add_error( is_wp_error( $return ) ? $return->get_error_message() : __('Invalid coupon.', 'woocommerce') );
return false; return false;
} }

View File

@ -118,8 +118,15 @@ class WC_Coupon {
$this->usage_count++; $this->usage_count++;
update_post_meta($this->id, 'usage_count', $this->usage_count); update_post_meta($this->id, 'usage_count', $this->usage_count);
} }
/** Check coupon is valid */ /**
* is_valid function.
*
* Check if a coupon is valid. Return a reason code if invaid. Reason codes:
*
* @access public
* @return void
*/
function is_valid() { function is_valid() {
global $woocommerce; global $woocommerce;
@ -127,11 +134,13 @@ class WC_Coupon {
if ($this->id) : if ($this->id) :
$valid = true; $valid = true;
$error = false;
// Usage Limit // Usage Limit
if ($this->usage_limit>0) : if ($this->usage_limit>0) :
if ($this->usage_count>=$this->usage_limit) : if ($this->usage_count>=$this->usage_limit) :
$valid = false; $valid = false;
$error = __( 'Coupon usage limit has been reached.', 'woocommerce' );
endif; endif;
endif; endif;
@ -139,6 +148,7 @@ class WC_Coupon {
if ($this->expiry_date) : if ($this->expiry_date) :
if (strtotime('NOW')>$this->expiry_date) : if (strtotime('NOW')>$this->expiry_date) :
$valid = false; $valid = false;
$error = __( 'This coupon has expired.', 'woocommerce' );
endif; endif;
endif; endif;
@ -146,6 +156,7 @@ class WC_Coupon {
if ($this->minimum_amount>0) : if ($this->minimum_amount>0) :
if ( $this->minimum_amount > $woocommerce->cart->subtotal ) : if ( $this->minimum_amount > $woocommerce->cart->subtotal ) :
$valid = false; $valid = false;
$error = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), $this->minimum_amount );
endif; endif;
endif; endif;
@ -157,7 +168,7 @@ class WC_Coupon {
$valid_for_cart = true; $valid_for_cart = true;
endif; endif;
endforeach; endif; endforeach; endif;
if (!$valid_for_cart) $valid = false; if ( ! $valid_for_cart ) $valid = false;
endif; endif;
// Category ids - If a product included is found in the cart then its valid // Category ids - If a product included is found in the cart then its valid
@ -170,7 +181,7 @@ class WC_Coupon {
if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) $valid_for_cart = true; if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) $valid_for_cart = true;
endforeach; endif; endforeach; endif;
if (!$valid_for_cart) $valid = false; if ( ! $valid_for_cart ) $valid = false;
endif; endif;
// Cart discounts cannot be added if non-eligble product is found in cart // Cart discounts cannot be added if non-eligble product is found in cart
@ -184,7 +195,7 @@ class WC_Coupon {
$valid_for_cart = false; $valid_for_cart = false;
endif; endif;
endforeach; endif; endforeach; endif;
if (!$valid_for_cart) $valid = false; if ( ! $valid_for_cart ) $valid = false;
endif; endif;
// Exclude Categories // Exclude Categories
@ -197,18 +208,18 @@ class WC_Coupon {
if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 ) $valid_for_cart = false; if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 ) $valid_for_cart = false;
endforeach; endif; endforeach; endif;
if (!$valid_for_cart) $valid = false; if ( ! $valid_for_cart ) $valid = false;
endif; endif;
endif; endif;
$valid = apply_filters('woocommerce_coupon_is_valid', $valid, $this); $valid = apply_filters( 'woocommerce_coupon_is_valid', $valid, $this );
if ($valid) return true; if ( $valid ) return true;
endif; endif;
return false; return new WP_Error( 'coupon_error', apply_filters( 'woocommerce_coupon_error', $error, $this ) );
} }
} }

View File

@ -153,6 +153,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Feature - Menu count for orders requiring admin action * Feature - Menu count for orders requiring admin action
* Feature - 'supports' function for gateways. * Feature - 'supports' function for gateways.
* Feature - Ajax powered coupon form on checkout. * Feature - Ajax powered coupon form on checkout.
* Tweak - Improved coupon feedback messages
* Tweak - woocommerce_get_product_terms for getting terms in the user defined order. * Tweak - woocommerce_get_product_terms for getting terms in the user defined order.
* Tweak - Variations that are disabled are not taken into consideration when displaying parent price. * Tweak - Variations that are disabled are not taken into consideration when displaying parent price.
* Tweak - Variations maintain selections after adding to cart. * Tweak - Variations maintain selections after adding to cart.