A different attempt

This commit is contained in:
Ken Bass 2013-02-14 22:56:35 -05:00
parent b0a5402f11
commit fe8a34a069
4 changed files with 149 additions and 40 deletions

View File

@ -342,8 +342,8 @@ class WC_Cart {
$coupon = new WC_Coupon( $code ); $coupon = new WC_Coupon( $code );
if ( is_wp_error( $coupon->is_valid() ) ) { if ( is_wp_error( $coupon->is_valid() ) ) {
$woocommerce->add_error( sprintf( __( 'Sorry, it seems the %s "%s" is invalid - it has now been removed from your order.', 'woocommerce' ), $coupon->coupon_text, $code ) ); $coupon->add_coupon_message( WC_Coupon::E_COUPON_INVALID_REMOVED );
// Remove the coupon // Remove the coupon
unset( $this->applied_coupons[ $key ] ); unset( $this->applied_coupons[ $key ] );
@ -420,7 +420,7 @@ class WC_Cart {
$check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) ); $check_emails = array_map( 'sanitize_email', array_map( 'strtolower', $check_emails ) );
if ( 0 == sizeof( array_intersect( $check_emails, $coupon->customer_email ) ) ) { if ( 0 == sizeof( array_intersect( $check_emails, $coupon->customer_email ) ) ) {
$woocommerce->add_error( sprintf( __( 'Sorry, it seems the %s "%s" is not yours - it has now been removed from your order.', 'woocommerce' ), $coupon->coupon_text, $code ) ); $coupon->add_coupon_message( WC_Coupon::E_COUPON_NOT_YOURS_REMOVED );
// Remove the coupon // Remove the coupon
unset( $this->applied_coupons[ $key ] ); unset( $this->applied_coupons[ $key ] );
@ -1767,7 +1767,7 @@ class WC_Cart {
// Check if applied // Check if applied
if ( $woocommerce->cart->has_discount( $coupon_code ) ) { if ( $woocommerce->cart->has_discount( $coupon_code ) ) {
$woocommerce->add_error( sprintf( __( '%s already applied!', 'woocommerce' ), ucfirst( $the_coupon->coupon_text ) ) ); $the_coupon->add_coupon_message( WC_Coupon::E_COUPON_ALREADY_APPLIED );
return false; return false;
} }
@ -1784,7 +1784,7 @@ class WC_Cart {
if ( $existing_coupon->individual_use == 'yes' && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $existing_coupon, $this->applied_coupons ) ) { if ( $existing_coupon->individual_use == 'yes' && false === apply_filters( 'woocommerce_apply_with_individual_use_coupon', false, $the_coupon, $existing_coupon, $this->applied_coupons ) ) {
// Reject new coupon // Reject new coupon
$woocommerce->add_error( sprintf( __( 'Sorry, %s <code>%s</code> has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ), $existing_coupon->coupon_text, $code ) ); $existing_coupon->add_coupon_message( WC_Coupon::E_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY );
return false; return false;
} }
@ -1799,15 +1799,15 @@ class WC_Cart {
} }
$this->calculate_totals(); $this->calculate_totals();
$woocommerce->add_message( sprintf( __( '%s applied successfully.', 'woocommerce' ), ucfirst( $the_coupon->coupon_text ) ) );
$the_coupon->add_coupon_message( WC_Coupon::I_COUPON_SUCCESS );
do_action( 'woocommerce_applied_coupon', $coupon_code ); do_action( 'woocommerce_applied_coupon', $coupon_code );
return true; return true;
} else { } else {
$woocommerce->add_error( sprintf( __( '%s does not exist!', 'woocommerce' ), ucfirst( $the_coupon->coupon_text ) ) ); $the_coupon->add_coupon_message( WC_Coupon::E_COUPON_NOT_EXIST );
return false; return false;
} }
return false; return false;

View File

@ -11,6 +11,24 @@
*/ */
class WC_Coupon { class WC_Coupon {
// Coupon error message codes
const E_COUPON_MIN_ERROR_CODE = 100;
const E_COUPON_INVALID_REMOVED = 100;
const E_COUPON_NOT_YOURS_REMOVED = 101;
const E_COUPON_ALREADY_APPLIED = 102;
const E_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 103;
const E_COUPON_NOT_EXIST = 104;
const E_COUPON_USAGE_LIMIT_REACHED = 105;
const E_COUPON_EXPIRED = 106;
const E_COUPON_MIN_SPEND_LIMIT_NOT_MET = 107;
const E_COUPON_NOT_APPLICABLE = 108;
const E_COUPON_NOT_VALID_SALE_ITEMS = 109;
const E_COUPON_PLEASE_ENTER = 110;
// Coupon info message codes
const I_COUPON_MIN_SUCCESS_CODE = 200;
const I_COUPON_SUCCESS = 200;
/** @public string Coupon code. */ /** @public string Coupon code. */
public $code; public $code;
@ -70,9 +88,6 @@ class WC_Coupon {
/** @public string Error message. */ /** @public string Error message. */
public $error_message; public $error_message;
/** @public string Filterable text used on frontend - defaults to 'coupon code'. */
public $coupon_text;
/** /**
* Coupon constructor. Loads coupon data. * Coupon constructor. Loads coupon data.
@ -113,11 +128,8 @@ class WC_Coupon {
$coupon_id = $wpdb->get_var( $wpdb->prepare( apply_filters( 'woocommerce_coupon_code_query', "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon'" ), $this->code ) ); $coupon_id = $wpdb->get_var( $wpdb->prepare( apply_filters( 'woocommerce_coupon_code_query', "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon'" ), $this->code ) );
if ( ! $coupon_id ) { if ( ! $coupon_id )
// Handle the case where coupon does not exist. Filter can decode whether to call it something generic or more specific
$this->coupon_text = apply_filters( 'woocommerce_coupon_text', __( 'coupon code', 'woocommerce' ), $this );
return; return;
}
$coupon = get_post( $coupon_id ); $coupon = get_post( $coupon_id );
$coupon->post_title = apply_filters( 'woocommerce_coupon_code', $coupon->post_title ); $coupon->post_title = apply_filters( 'woocommerce_coupon_code', $coupon->post_title );
@ -161,9 +173,6 @@ class WC_Coupon {
$this->exclude_product_categories = array_filter( array_map( 'trim', (array) maybe_unserialize( $this->exclude_product_categories ) ) ); $this->exclude_product_categories = array_filter( array_map( 'trim', (array) maybe_unserialize( $this->exclude_product_categories ) ) );
$this->customer_email = array_filter( array_map( 'trim', array_map( 'strtolower', (array) maybe_unserialize( $this->customer_email ) ) ) ); $this->customer_email = array_filter( array_map( 'trim', array_map( 'strtolower', (array) maybe_unserialize( $this->customer_email ) ) ) );
} }
$this->coupon_text = apply_filters( 'woocommerce_coupon_text', __( 'coupon code', 'woocommerce' ), $this );
} }
@ -245,6 +254,8 @@ class WC_Coupon {
public function is_valid() { public function is_valid() {
global $woocommerce; global $woocommerce;
$error_code = null;
if ( $this->id ) { if ( $this->id ) {
$valid = true; $valid = true;
@ -254,7 +265,7 @@ class WC_Coupon {
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 = sprintf( __( '%s usage limit has been reached.', 'woocommerce' ), ucfirst( $this->coupon_text ) ); $error_code = self::E_COUPON_USAGE_LIMIT_REACHED;
} }
} }
@ -262,15 +273,15 @@ class WC_Coupon {
if ( $this->expiry_date ) { if ( $this->expiry_date ) {
if ( current_time( 'timestamp' ) > $this->expiry_date ) { if ( current_time( 'timestamp' ) > $this->expiry_date ) {
$valid = false; $valid = false;
$error = sprintf( __( 'This %s has expired.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_EXPIRED;
} }
} }
// Minimum spend // Minimum spend
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 %s is %s.', 'woocommerce' ), $this->coupon_text, woocommerce_price( $this->minimum_amount ) ); $error_code = self::E_COUPON_MIN_SPEND_LIMIT_NOT_MET;
} }
} }
@ -286,7 +297,7 @@ class WC_Coupon {
} }
if ( ! $valid_for_cart ) { if ( ! $valid_for_cart ) {
$valid = false; $valid = false;
$error = sprintf( __( 'Sorry, this %s is not applicable to your cart contents.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_NOT_APPLICABLE;
} }
} }
@ -304,7 +315,7 @@ class WC_Coupon {
} }
if ( ! $valid_for_cart ) { if ( ! $valid_for_cart ) {
$valid = false; $valid = false;
$error = sprintf( __( 'Sorry, this %s is not applicable to your cart contents.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_NOT_APPLICABLE;
} }
} }
@ -323,7 +334,7 @@ class WC_Coupon {
} }
if ( ! $valid_for_cart ) { if ( ! $valid_for_cart ) {
$valid = false; $valid = false;
$error = sprintf( __( 'Sorry, this %s is not applicable to your cart contents.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_NOT_APPLICABLE;
} }
} }
@ -340,7 +351,7 @@ class WC_Coupon {
} }
if ( ! $valid_for_cart ) { if ( ! $valid_for_cart ) {
$valid = false; $valid = false;
$error = sprintf( __( 'Sorry, this %s is not valid for sale items.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_NOT_VALID_SALE_ITEMS;
} }
} }
@ -358,7 +369,7 @@ class WC_Coupon {
} }
if ( ! $valid_for_cart ) { if ( ! $valid_for_cart ) {
$valid = false; $valid = false;
$error = sprintf( __( 'Sorry, this %s is not applicable to your cart contents.', 'woocommerce' ), $this->coupon_text ); $error_code = self::E_COUPON_NOT_APPLICABLE;
} }
} }
} }
@ -368,26 +379,122 @@ class WC_Coupon {
if ( $valid ) if ( $valid )
return true; return true;
} else { } else {
$error = sprintf( __( 'Invalid %s', 'woocommerce' ), self::get_generic_coupon_text() ); $error_code = self::E_COUPON_NOT_EXIST;
} }
if ($error_code) {
$error = $this->map_coupon_message( $error_code );
}
$this->error_message = apply_filters( 'woocommerce_coupon_error', $error, $this ); $this->error_message = apply_filters( 'woocommerce_coupon_error', $error, $this );
return false; return false;
} }
/**
* Converts one of the WC_Coupon message/error codes to a message string and
* displays the message/error.
*
* @access public
* @param int $msg_code Message/error code.
* @return void
*/
public function add_coupon_message( $msg_code ) {
global $woocommerce;
$msg = $this->map_coupon_message($msg_code);
if ($msg_code >= self::I_COUPON_MIN_SUCCESS_CODE) {
$woocommerce->add_message($msg);
} elseif ($msg_code >= self::E_COUPON_MIN_ERROR_CODE) {
$woocommerce->add_error($msg);
}
}
/**
* Map one of the WC_Coupon message/error codes to a message string
*
* @access public
* @param int $msg_code Message/error code.
* @return string| Message/error string
*/
public function map_coupon_message( $msg_code ) {
$code = $this->code;
switch ($msg_code) {
case self::E_COUPON_NOT_EXIST:
$msg = __( 'Coupon does not exist!', 'woocommerce' );
break;
case self::E_COUPON_INVALID_REMOVED:
$msg = sprintf( __( 'Sorry, it seems the coupon "%s" is invalid - it has now been removed from your order.', 'woocommerce' ), $code );
break;
case self::E_COUPON_NOT_YOURS_REMOVED:
$msg = sprintf( __( 'Sorry, it seems the coupon "%s" is not yours - it has now been removed from your order.', 'woocommerce' ), $code );
break;
case self::E_COUPON_ALREADY_APPLIED:
$msg = __( 'Coupon code already applied!', 'woocommerce' );
break;
case self::E_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY:
$msg = sprintf( __( 'Sorry, coupon <code>%s</code> has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ), $code );
break;
case self::E_COUPON_USAGE_LIMIT_REACHED:
$msg = __( 'Coupon usage limit has been reached.', 'woocommerce' );
break;
case self::E_COUPON_EXPIRED:
$msg = __( 'This coupon has expired.', 'woocommerce' );
break;
case self::E_COUPON_MIN_SPEND_LIMIT_NOT_MET:
$msg = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), woocommerce_price( $this->minimum_amount ) );
break;
case self::E_COUPON_NOT_APPLICABLE:
$msg = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
break;
case self::E_COUPON_NOT_VALID_SALE_ITEMS:
$msg = __( 'Sorry, this coupon is not valid for sale items.', 'woocommerce' );
break;
case self::I_COUPON_SUCCESS:
$msg = __( 'Coupon code applied successfully.', 'woocommerce' );
break;
default:
$msg = '';
break;
}
$msg = apply_filters( 'woocommerce_coupon_message', $msg, $msg_code, $this );
return $msg;
}
/** /**
* get_generic_coupon_text static function. * Map one of the WC_Coupon error codes to an error string
* * No coupon instance will be available where a coupon does not exist,
* Return generic coupon text string for when there is no coupon instance to be used in * so this static method exists.
* text strings such as error messages.
* *
* @access public * @access public
* @return string|Defaults to 'coupon code' * @param int $err_code Error code
* @return string| Error string
*/ */
public static function map_generic_coupon_error( $err_code ) {
public static function get_generic_coupon_text() { switch ($err_code) {
// Handle the case where coupon does not exist. Filter can decode whether to call it something generic case self::E_COUPON_NOT_EXIST:
return apply_filters( 'woocommerce_coupon_text', __( 'coupon code', 'woocommerce' ), null ); $error = __( 'Coupon does not exist!', 'woocommerce' );
break;
case self::E_COUPON_PLEASE_ENTER:
$error = __( 'Please enter a coupon code.', 'woocommerce' );
break;
default:
$error = '';
break;
}
// When using this static method, there is no $this to pass to filter
$error = apply_filters( 'woocommerce_coupon_message', $error, $err_code, null );
return $error;
} }
} }

View File

@ -31,7 +31,8 @@ class WC_Shortcode_Cart {
if ( ! empty( $_POST['coupon_code'] ) ) { if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) ); $woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
} else { } else {
$woocommerce->add_error( sprintf( __( 'Please enter a %s.', 'woocommerce' ), WC_Coupon::get_generic_coupon_text() ) ); $error = WC_Coupon::map_generic_coupon_error( WC_Coupon::E_COUPON_PLEASE_ENTER );
$woocommerce->add_error( $error );
} }
// Remove Coupon Codes // Remove Coupon Codes

View File

@ -131,7 +131,8 @@ function woocommerce_ajax_apply_coupon() {
if ( ! empty( $_POST['coupon_code'] ) ) { if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) ); $woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
} else { } else {
$woocommerce->add_error( sprintf( __( 'Please enter a %s.', 'woocommerce' ), WC_Coupon::get_generic_coupon_text() ) ); $error = WC_Coupon::map_generic_coupon_error( WC_Coupon::E_COUPON_PLEASE_ENTER );
$woocommerce->add_error( $error );
} }
$woocommerce->show_messages(); $woocommerce->show_messages();