Case insensitive coupons. Closes #1633.

This commit is contained in:
Mike Jolley 2012-11-08 16:57:53 +00:00
parent 3baaf6029a
commit 764b34a938
8 changed files with 144 additions and 118 deletions

View File

@ -170,7 +170,11 @@ function woocommerce_coupon_data_meta_box( $post ) {
*/
function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
global $wpdb, $woocommerce_errors;
// Ensure coupon code is correctly formatted
$post->post_title = apply_filters( 'woocommerce_coupon_code', $post->post_title );
$wpdb->update( $wpdb->posts, array( 'post_title' => $post->post_title ), array( 'ID' => $post_id ) );
// Check for dupe coupons
$coupon_found = $wpdb->get_var( $wpdb->prepare( "
SELECT $wpdb->posts.ID
@ -179,10 +183,10 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) {
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_title = '%s'
AND $wpdb->posts.ID != %s
", esc_attr( $_POST['post_title'] ), $post_id ) );
", $post->post_title, $post_id ) );
if ( $coupon_found )
$woocommerce_errors[] = __( 'Coupon code already exists.', 'woocommerce' );
$woocommerce_errors[] = __( 'Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce' );
// Add/Replace data to array
$type = woocommerce_clean( $_POST['discount_type'] );

View File

@ -117,15 +117,15 @@ add_filter('enter_title_here', 'woocommerce_enter_title_here', 1, 2);
* @return void
*/
function woocommerce_meta_boxes_save( $post_id, $post ) {
if ( empty($post_id) || empty($post) || empty($_POST) ) return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( empty( $post_id ) || empty( $post ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( is_int( wp_is_post_revision( $post ) ) ) return;
if ( is_int( wp_is_post_autosave( $post ) ) ) return;
if ( empty($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' )) return;
if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) return;
if ( !current_user_can( 'edit_post', $post_id )) return;
if ( $post->post_type != 'product' && $post->post_type != 'shop_order' && $post->post_type != 'shop_coupon' ) return;
do_action( 'woocommerce_process_'.$post->post_type.'_meta', $post_id, $post );
do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );
woocommerce_meta_boxes_save_errors();
}

View File

@ -1608,7 +1608,7 @@ class WC_Cart {
* @return bool
*/
function has_discount( $code ) {
if ( in_array($code, $this->applied_coupons) ) return true;
if ( in_array( $code, $this->applied_coupons ) ) return true;
return false;
}
@ -1624,7 +1624,7 @@ class WC_Cart {
// Coupons are globally disabled
if ( get_option('woocommerce_enable_coupons') == 'no' ) return false;
$the_coupon = new WC_Coupon($coupon_code);
$the_coupon = new WC_Coupon( $coupon_code );
if ( $the_coupon->id ) {

View File

@ -75,73 +75,81 @@ class WC_Coupon {
function __construct( $code ) {
global $wpdb;
$this->code = esc_attr($code);
$this->code = apply_filters( 'woocommerce_coupon_code', $code );
// Coupon data lets developers create coupons through code
$coupon_data = apply_filters( 'woocommerce_get_shop_coupon_data', false, $code );
$coupon_data = apply_filters('woocommerce_get_shop_coupon_data', false, $code);
if ($coupon_data) :
$this->id = absint( $coupon_data['id'] );
$this->type = esc_html( $coupon_data['type'] );
$this->amount = esc_html( $coupon_data['amount'] );
$this->individual_use = esc_html( $coupon_data['individual_use'] );
$this->product_ids = ( is_array( $coupon_data['product_ids'] ) ) ? $coupon_data['product_ids'] : array();
$this->exclude_product_ids = ( is_array( $coupon_data['exclude_product_ids'] ) ) ? $coupon_data['exclude_product_ids'] : array();
$this->usage_limit = absint( $coupon_data['usage_limit'] );
$this->usage_count = absint( $coupon_data['usage_count'] );
$this->expiry_date = esc_html( $coupon_data['expiry_date'] );
$this->apply_before_tax = esc_html( $coupon_data['apply_before_tax'] );
$this->free_shipping = esc_html( $coupon_data['free_shipping'] );
$this->product_categories = ( is_array( $coupon_data['product_categories'] ) ) ? $coupon_data['product_categories'] : array();
$this->exclude_product_categories = ( is_array( $coupon_data['exclude_product_categories'] ) ) ? $coupon_data['exclude_product_categories'] : array();
$this->minimum_amount = esc_html( $coupon_data['minimum_amount'] );
$this->customer_email = esc_html( $coupon_data['customer_email'] );
if ( $coupon_data ) {
$this->id = absint( $coupon_data['id'] );
$this->type = esc_html( $coupon_data['type'] );
$this->amount = esc_html( $coupon_data['amount'] );
$this->individual_use = esc_html( $coupon_data['individual_use'] );
$this->product_ids = is_array( $coupon_data['product_ids'] ) ? $coupon_data['product_ids'] : array();
$this->exclude_product_ids = is_array( $coupon_data['exclude_product_ids'] ) ? $coupon_data['exclude_product_ids'] : array();
$this->usage_limit = absint( $coupon_data['usage_limit'] );
$this->usage_count = absint( $coupon_data['usage_count'] );
$this->expiry_date = esc_html( $coupon_data['expiry_date'] );
$this->apply_before_tax = esc_html( $coupon_data['apply_before_tax'] );
$this->free_shipping = esc_html( $coupon_data['free_shipping'] );
$this->product_categories = is_array( $coupon_data['product_categories'] ) ? $coupon_data['product_categories'] : array();
$this->exclude_product_categories = is_array( $coupon_data['exclude_product_categories'] ) ? $coupon_data['exclude_product_categories'] : array();
$this->minimum_amount = esc_html( $coupon_data['minimum_amount'] );
$this->customer_email = esc_html( $coupon_data['customer_email'] );
return true;
else:
$coupon_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE BINARY post_title = %s AND post_type= %s", $this->code, 'shop_coupon' ) );
if ( $coupon_id ) $coupon = get_page($coupon_id); else return false;
} else {
$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 )
$coupon = get_post( $coupon_id );
$coupon->post_title = apply_filters( 'woocommerce_coupon_code', $coupon->post_title );
// Check titles match
if ($this->code!==$coupon->post_title) return false;
if ( empty( $coupon ) || $coupon->post_status !== 'publish' || $this->code !== $coupon->post_title )
return false;
if ($coupon && $coupon->post_status == 'publish') :
$this->id = $coupon->ID;
$this->coupon_custom_fields = get_post_custom( $this->id );
$this->id = $coupon->ID;
$this->coupon_custom_fields = get_post_custom( $this->id );
$load_data = array(
'discount_type' => 'fixed_cart',
'coupon_amount' => 0,
'individual_use' => 'no',
'product_ids' => '',
'exclude_product_ids' => '',
'usage_limit' => '',
'usage_count' => '',
'expiry_date' => '',
'apply_before_tax' => 'yes',
'free_shipping' => 'no',
'product_categories' => array(),
'exclude_product_categories' => array(),
'minimum_amount' => '',
'customer_email' => array()
);
$load_data = array(
'discount_type' => 'fixed_cart',
'coupon_amount' => 0,
'individual_use' => 'no',
'product_ids' => '',
'exclude_product_ids' => '',
'usage_limit' => '',
'usage_count' => '',
'expiry_date' => '',
'apply_before_tax' => 'yes',
'free_shipping' => 'no',
'product_categories' => array(),
'exclude_product_categories' => array(),
'minimum_amount' => '',
'customer_email' => array()
);
foreach ($load_data as $key => $default) $this->$key = (isset($this->coupon_custom_fields[$key][0]) && $this->coupon_custom_fields[$key][0]!=='') ? $this->coupon_custom_fields[$key][0] : $default;
foreach ( $load_data as $key => $default )
$this->$key = isset( $this->coupon_custom_fields[ $key ][0] ) && $this->coupon_custom_fields[ $key ][0] !== '' ? $this->coupon_custom_fields[ $key ][0] : $default;
// Alias
$this->type = $this->discount_type;
$this->amount = $this->coupon_amount;
// Alias
$this->type = $this->discount_type;
$this->amount = $this->coupon_amount;
// Formatting
$this->product_ids = array_filter(array_map('trim', explode(',', $this->product_ids)));
$this->exclude_product_ids = array_filter(array_map('trim', explode(',', $this->exclude_product_ids)));
$this->expiry_date = ($this->expiry_date) ? strtotime($this->expiry_date) : '';
$this->product_categories = array_filter(array_map('trim', (array) maybe_unserialize($this->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))));
return true;
endif;
endif;
// Formatting
$this->product_ids = array_filter( array_map( 'trim', explode( ',', $this->product_ids ) ) );
$this->exclude_product_ids = array_filter( array_map( 'trim', explode( ',', $this->exclude_product_ids ) ) );
$this->expiry_date = $this->expiry_date ? strtotime( $this->expiry_date ) : '';
$this->product_categories = array_filter( array_map( 'trim', (array) maybe_unserialize( $this->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 ) ) ) );
return true;
}
return false;
}
@ -153,7 +161,7 @@ class WC_Coupon {
* @return bool
*/
function apply_before_tax() {
return ($this->apply_before_tax=='yes') ? true : false;
return $this->apply_before_tax == 'yes' ? true : false;
}
@ -164,7 +172,7 @@ class WC_Coupon {
* @return void
*/
function enable_free_shipping() {
return ($this->free_shipping=='yes') ? true : false;
return $this->free_shipping == 'yes' ? true : false;
}
@ -208,93 +216,100 @@ class WC_Coupon {
$error = false;
// Usage Limit
if ($this->usage_limit>0) :
if ($this->usage_count>=$this->usage_limit) :
if ( $this->usage_limit > 0 ) {
if ( $this->usage_count >= $this->usage_limit ) {
$valid = false;
$error = __( 'Coupon usage limit has been reached.', 'woocommerce' );
endif;
endif;
}
}
// Expired
if ($this->expiry_date) :
if (strtotime('NOW')>$this->expiry_date) :
if ( $this->expiry_date ) {
if ( strtotime( 'NOW' ) > $this->expiry_date ) {
$valid = false;
$error = __( 'This coupon has expired.', 'woocommerce' );
endif;
endif;
}
}
// Minimum spend
if ($this->minimum_amount>0) :
if ( $this->minimum_amount > $woocommerce->cart->subtotal ) :
if ( $this->minimum_amount > 0 ) {
if ( $this->minimum_amount > $woocommerce->cart->subtotal ) {
$valid = false;
$error = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), woocommerce_price( $this->minimum_amount ) );
endif;
endif;
}
}
// Product ids - If a product included is found in the cart then its valid
if (sizeof( $this->product_ids )>0) :
if ( sizeof( $this->product_ids ) > 0 ) {
$valid_for_cart = false;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
if (in_array($cart_item['product_id'], $this->product_ids) || in_array($cart_item['variation_id'], $this->product_ids) || in_array($cart_item['data']->get_parent(), $this->product_ids)) :
$valid_for_cart = true;
endif;
endforeach; endif;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->product_ids ) || in_array( $cart_item['variation_id'], $this->product_ids ) || in_array( $cart_item['data']->get_parent(), $this->product_ids ) )
$valid_for_cart = true;
}
}
if ( ! $valid_for_cart ) {
$valid = false;
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
}
endif;
}
// Category ids - If a product included is found in the cart then its valid
if (sizeof( $this->product_categories )>0) :
if ( sizeof( $this->product_categories ) > 0 ) {
$valid_for_cart = false;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
$product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array("fields" => "ids"));
if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) $valid_for_cart = true;
endforeach; endif;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array("fields" => "ids"));
if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 )
$valid_for_cart = true;
}
}
if ( ! $valid_for_cart ) {
$valid = false;
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
}
endif;
}
// Cart discounts cannot be added if non-eligble product is found in cart
if ($this->type!='fixed_product' && $this->type!='percent_product') :
if ( $this->type != 'fixed_product' && $this->type != 'percent_product' ) {
// Exclude Products
if (sizeof( $this->exclude_product_ids )>0) :
if ( sizeof( $this->exclude_product_ids ) > 0 ) {
$valid_for_cart = true;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
if (in_array($cart_item['product_id'], $this->exclude_product_ids) || in_array($cart_item['variation_id'], $this->exclude_product_ids) || in_array($cart_item['data']->get_parent(), $this->exclude_product_ids)) :
$valid_for_cart = false;
endif;
endforeach; endif;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->exclude_product_ids ) || in_array( $cart_item['variation_id'], $this->exclude_product_ids ) || in_array( $cart_item['data']->get_parent(), $this->exclude_product_ids ) ) {
$valid_for_cart = false;
}
}
}
if ( ! $valid_for_cart ) {
$valid = false;
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
}
endif;
}
// Exclude Categories
if (sizeof( $this->exclude_product_categories )>0) :
if ( sizeof( $this->exclude_product_categories ) > 0 ) {
$valid_for_cart = true;
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) :
$product_cats = wp_get_post_terms($cart_item['product_id'], 'product_cat', array("fields" => "ids"));
if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 ) $valid_for_cart = false;
endforeach; endif;
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array( "fields" => "ids" ) );
if ( sizeof( array_intersect( $product_cats, $this->exclude_product_categories ) ) > 0 )
$valid_for_cart = false;
}
}
if ( ! $valid_for_cart ) {
$valid = false;
$error = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
}
endif;
endif;
}
}
$valid = apply_filters( 'woocommerce_coupon_is_valid', $valid, $this );

View File

@ -209,6 +209,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Exclude up-sells from related products.
* Tweak - Allowed sku search to return > 1 result.
* Tweak - If only one country is enabled, don't show country dropdown on checkout.
* Tweak - Case insensitive coupons.
* Fix - Added more error messages for coupons.
* Fix - Variation sku updating after selection.

View File

@ -41,7 +41,7 @@ function woocommerce_cart( $atts ) {
if ( ! empty( $_POST['apply_coupon'] ) ) {
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( stripslashes( trim( $_POST['coupon_code'] ) ) );
$woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
} else {
$woocommerce->add_error( __( 'Please enter a coupon code.', 'woocommerce' ) );
}

View File

@ -95,7 +95,7 @@ function woocommerce_ajax_apply_coupon() {
check_ajax_referer( 'apply-coupon', 'security' );
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( sanitize_text_field( trim( $_POST['coupon_code'] ) ) );
$woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
} else {
$woocommerce->add_error( __( 'Please enter a coupon code.', 'woocommerce' ) );
}

View File

@ -12,6 +12,12 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Hooks used in admin and frontend
*/
add_filter( 'woocommerce_coupon_code', 'sanitize_text_field' );
add_filter( 'woocommerce_coupon_code', 'strtolower' ); // Coupons case-insensitive by default
/**
* woocommerce_get_dimension function.
*