From db6947715e41b13033c3bc6748b0ff5867427588 Mon Sep 17 00:00:00 2001 From: marcusds Date: Wed, 16 Apr 2014 23:20:36 -0700 Subject: [PATCH 1/2] Check all coupon that use the same code Instead of just checking the first code and failing if it doesn't work, check all codes until we find the first that works. --- wpsc-includes/coupons.class.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/wpsc-includes/coupons.class.php b/wpsc-includes/coupons.class.php index ddd9eb69f8..391d762be5 100644 --- a/wpsc-includes/coupons.class.php +++ b/wpsc-includes/coupons.class.php @@ -53,25 +53,22 @@ class wpsc_coupons { * Coupons constractor * * Instantiate a coupons object with optional variable $code; + * If there are more than one coupon that matches $code check them all to find the first that is valid, if not are return false. * * @param string code (optional) the coupon code you would like to use. * @return bool True if coupon code exists, False otherwise. */ - function wpsc_coupons($code = ''){ + function wpsc_coupons($code = '') { global $wpdb; if ( empty( $code ) ) return false; $this->code = $code; - - $coupon_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_COUPON_CODES."` WHERE coupon_code = %s LIMIT 1", $code ) , ARRAY_A ); - - if ( ( $coupon_data == '' ) || ( $coupon_data == null ) || ( strtotime( $coupon_data['expiry'] ) < time() ) ) { - $this->errormsg = true; - wpsc_delete_customer_meta( 'coupon' ); - return false; - } else { + + $coupons_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_COUPON_CODES."` WHERE coupon_code = %s", $code ) , ARRAY_A ); + + foreach($coupons_data as $key => $coupon_data) { $coupon_data = array_merge( array( 'value' => '', 'is-percentage' => '', @@ -95,10 +92,18 @@ function wpsc_coupons($code = ''){ $this->every_product = $coupon_data['every_product']; $this->errormsg = false; $valid = $this->validate_coupon(); + $items = $this->get_eligible_items(); - return $valid; + if ( empty( $items ) ) + continue; + + if($valid) + return true; } - + + $this->errormsg = true; + wpsc_delete_customer_meta( 'coupon' ); + return false; } /** @@ -315,12 +320,10 @@ public function _filter_cart_item_conditions( $cart_item ) { } else { - /* This allows for a function outside of this class to override a custom condition. */ if ( function_exists( $callback ) ) { $result = $callback( $condition, $cart_item ); } else { - /* This allows for a plugin to create a condition callback for the condition. Perk: doesn't have to follow $callback nomenclature. */ - $result = apply_filters( 'wpsc_coupon_conditions_default_callback', false, $callback, $condition, $cart_item ); + $result = apply_filters( 'wpsc_coupon_default_callback', false, $callback, $cart_item ); } } @@ -758,4 +761,4 @@ function uses_coupons() { } -?> +?> \ No newline at end of file From 5a53b7ed3470400af2ef32637095d8fe95223db9 Mon Sep 17 00:00:00 2001 From: marcusds Date: Wed, 16 Apr 2014 23:34:40 -0700 Subject: [PATCH 2/2] Fixing upstream changes --- wpsc-includes/coupons.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wpsc-includes/coupons.class.php b/wpsc-includes/coupons.class.php index 391d762be5..8dcd80be3a 100644 --- a/wpsc-includes/coupons.class.php +++ b/wpsc-includes/coupons.class.php @@ -53,7 +53,6 @@ class wpsc_coupons { * Coupons constractor * * Instantiate a coupons object with optional variable $code; - * If there are more than one coupon that matches $code check them all to find the first that is valid, if not are return false. * * @param string code (optional) the coupon code you would like to use. * @return bool True if coupon code exists, False otherwise. @@ -69,6 +68,10 @@ function wpsc_coupons($code = '') { $coupons_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `".WPSC_TABLE_COUPON_CODES."` WHERE coupon_code = %s", $code ) , ARRAY_A ); foreach($coupons_data as $key => $coupon_data) { + + if ( ( $coupon_data == '' ) || ( $coupon_data == null ) || ( strtotime( $coupon_data['expiry'] ) < time() ) ) + continue; + $coupon_data = array_merge( array( 'value' => '', 'is-percentage' => '', @@ -320,10 +323,12 @@ public function _filter_cart_item_conditions( $cart_item ) { } else { + /* This allows for a function outside of this class to override a custom condition. */ if ( function_exists( $callback ) ) { $result = $callback( $condition, $cart_item ); } else { - $result = apply_filters( 'wpsc_coupon_default_callback', false, $callback, $cart_item ); + /* This allows for a plugin to create a condition callback for the condition. Perk: doesn't have to follow $callback nomenclature. */ + $result = apply_filters( 'wpsc_coupon_conditions_default_callback', false, $callback, $condition, $cart_item ); } }