Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check all coupon that use the same code #1114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions wpsc-includes/coupons.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ class wpsc_coupons {
* @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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be spaced out a bit more:

$wpdb->prepare( 'SELECT * FROM ' . WPSC_TABLE_COUPON_CODES . ' WHERE coupon_code = %s', $code )

foreach($coupons_data as $key => $coupon_data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing here, too. See http://make.wordpress.org/core/handbook/coding-standards/php/#space-usage
foreach( $coupons_data as $key => $coupon_data ) {


if ( ( $coupon_data == '' ) || ( $coupon_data == null ) || ( strtotime( $coupon_data['expiry'] ) < time() ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be done with yoda conditionals.
if ( ( '' === $coupon_data ) || ( null === $coupon_data ) || ( strtotime( $coupon_data['expiry'] ) < time() ) )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, consider removing some of the unnecessary brackets:
if ( '' === $coupon_data || null === $coupon_data || strtotime( $coupon_data['expiry'] ) < time() )

continue;
$coupon_data = array_merge( array(
'value' => '',
'is-percentage' => '',
Expand All @@ -95,10 +95,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be wrapped with braces.

if ( empty( $items ) ) {
    continue;
}

See http://make.wordpress.org/core/handbook/coding-standards/php/#brace-style.


if($valid)
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

if ( $valid ) {
    return true;
}

}


$this->errormsg = true;
wpsc_delete_customer_meta( 'coupon' );
return false;
}

/**
Expand Down Expand Up @@ -758,4 +766,4 @@ function uses_coupons() {


}
?>
?>