Merge pull request #19456 from woocommerce/fix/coupon-class-phpcs

Fixed includes/class-wc-coupon.php PHPCS violations
This commit is contained in:
Mike Jolley 2018-03-20 16:46:25 +00:00 committed by GitHub
commit b51626f183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 213 additions and 185 deletions

View File

@ -1,25 +1,25 @@
<?php
include_once( 'legacy/class-wc-legacy-coupon.php' );
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WooCommerce coupons.
*
* The WooCommerce coupons class gets coupon data from storage and checks coupon validity.
*
* @class WC_Coupon
* @version 3.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
* @package WooCommerce/Classes
* @version 3.0.0
*/
defined( 'ABSPATH' ) || exit;
require_once dirname( __FILE__ ) . '/legacy/class-wc-legacy-coupon.php';
/**
* Coupon class.
*/
class WC_Coupon extends WC_Legacy_Coupon {
/**
* Data array, with defaults.
*
* @since 3.0.0
* @var array
*/
@ -49,7 +49,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
'virtual' => false,
);
// Coupon message codes
// Coupon message codes.
const E_WC_COUPON_INVALID_FILTERED = 100;
const E_WC_COUPON_INVALID_REMOVED = 101;
const E_WC_COUPON_NOT_YOURS_REMOVED = 102;
@ -62,7 +62,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
const E_WC_COUPON_NOT_APPLICABLE = 109;
const E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110;
const E_WC_COUPON_PLEASE_ENTER = 111;
const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112;
const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112;
const E_WC_COUPON_EXCLUDED_PRODUCTS = 113;
const E_WC_COUPON_EXCLUDED_CATEGORIES = 114;
const WC_COUPON_SUCCESS = 200;
@ -70,12 +70,14 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Cache group.
*
* @var string
*/
protected $cache_group = 'coupons';
/**
* Coupon constructor. Loads coupon data.
*
* @param mixed $data Coupon data, object, ID or code.
*/
public function __construct( $data = '' ) {
@ -83,7 +85,10 @@ class WC_Coupon extends WC_Legacy_Coupon {
if ( $data instanceof WC_Coupon ) {
$this->set_id( absint( $data->get_id() ) );
} elseif ( $coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $data ) ) {
}
$coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $data );
if ( $coupon ) {
$this->read_manual_coupon( $data, $coupon );
return;
} elseif ( is_int( $data ) && 'shop_coupon' === get_post_type( $data ) ) {
@ -110,11 +115,12 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Checks the coupon type.
* @param string $type Array or string of types
*
* @param string $type Array or string of types.
* @return bool
*/
public function is_type( $type ) {
return ( $this->get_discount_type() === $type || ( is_array( $type ) && in_array( $this->get_discount_type(), $type ) ) );
return ( $this->get_discount_type() === $type || ( is_array( $type ) && in_array( $this->get_discount_type(), $type, true ) ) );
}
/**
@ -128,18 +134,19 @@ class WC_Coupon extends WC_Legacy_Coupon {
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the coupon object.
|
*/
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the coupon object.
|
*/
/**
* Get coupon code.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_code( $context = 'view' ) {
@ -148,8 +155,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon description.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_description( $context = 'view' ) {
@ -158,8 +166,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get discount type.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_discount_type( $context = 'view' ) {
@ -168,8 +177,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon amount.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return float
*/
public function get_amount( $context = 'view' ) {
@ -178,8 +188,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon expiration date.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_expires( $context = 'view' ) {
@ -188,8 +199,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get date_created
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_created( $context = 'view' ) {
@ -198,8 +210,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get date_modified
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_modified( $context = 'view' ) {
@ -208,8 +221,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon usage count.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return integer
*/
public function get_usage_count( $context = 'view' ) {
@ -218,8 +232,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get the "indvidual use" checkbox status.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return bool
*/
public function get_individual_use( $context = 'view' ) {
@ -228,8 +243,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get product IDs this coupon can apply to.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_product_ids( $context = 'view' ) {
@ -238,8 +254,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get product IDs that this coupon should not apply to.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_excluded_product_ids( $context = 'view' ) {
@ -248,8 +265,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon usage limit.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return integer
*/
public function get_usage_limit( $context = 'view' ) {
@ -258,8 +276,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get coupon usage limit per customer (for a single customer)
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return integer
*/
public function get_usage_limit_per_user( $context = 'view' ) {
@ -268,8 +287,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Usage limited to certain amount of items
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return integer|null
*/
public function get_limit_usage_to_x_items( $context = 'view' ) {
@ -278,8 +298,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* If this coupon grants free shipping or not.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return bool
*/
public function get_free_shipping( $context = 'view' ) {
@ -288,8 +309,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get product categories this coupon can apply to.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_product_categories( $context = 'view' ) {
@ -298,8 +320,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get product categories this coupon cannot not apply to.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_excluded_product_categories( $context = 'view' ) {
@ -308,8 +331,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* If this coupon should exclude items on sale.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return bool
*/
public function get_exclude_sale_items( $context = 'view' ) {
@ -318,8 +342,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get minimum spend amount.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return float
*/
public function get_minimum_amount( $context = 'view' ) {
@ -327,8 +352,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
}
/**
* Get maximum spend amount.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return float
*/
public function get_maximum_amount( $context = 'view' ) {
@ -337,8 +363,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get emails to check customer usage restrictions.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_email_restrictions( $context = 'view' ) {
@ -347,8 +374,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get records of all users who have used the current coupon.
*
* @since 3.0.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return array
*/
public function get_used_by( $context = 'view' ) {
@ -359,7 +387,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* If the filter is added through the woocommerce_get_shop_coupon_data filter, it's virtual and not in the DB.
*
* @since 3.2.0
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return boolean
*/
public function get_virtual( $context = 'view' ) {
@ -369,10 +397,10 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Get discount amount for a cart item.
*
* @param float $discounting_amount Amount the coupon is being applied to
* @param array|null $cart_item Cart item being discounted if applicable
* @param boolean $single True if discounting a single qty item, false if its the line
* @return float Amount this coupon has discounted
* @param float $discounting_amount Amount the coupon is being applied to.
* @param array|null $cart_item Cart item being discounted if applicable.
* @param boolean $single True if discounting a single qty item, false if its the line.
* @return float Amount this coupon has discounted.
*/
public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) {
$discount = 0;
@ -406,21 +434,21 @@ class WC_Coupon extends WC_Legacy_Coupon {
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Functions for setting coupon data. These should not update anything in the
| database itself and should only change what is stored in the class
| object.
|
*/
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Functions for setting coupon data. These should not update anything in the
| database itself and should only change what is stored in the class
| object.
|
*/
/**
* Set coupon code.
* @since 3.0.0
* @param string $code
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param string $code Coupon code.
*/
public function set_code( $code ) {
$this->set_prop( 'code', wc_format_coupon_code( $code ) );
@ -428,9 +456,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set coupon description.
* @since 3.0.0
* @param string $description
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param string $description Description.
*/
public function set_description( $description ) {
$this->set_prop( 'description', $description );
@ -438,15 +466,15 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set discount type.
* @since 3.0.0
* @param string $discount_type
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param string $discount_type Discount type.
*/
public function set_discount_type( $discount_type ) {
if ( 'percent_product' === $discount_type ) {
$discount_type = 'percent'; // Backwards compatibility.
}
if ( ! in_array( $discount_type, array_keys( wc_get_coupon_types() ) ) ) {
if ( ! in_array( $discount_type, array_keys( wc_get_coupon_types() ), true ) ) {
$this->error( 'coupon_invalid_discount_type', __( 'Invalid discount type', 'woocommerce' ) );
}
$this->set_prop( 'discount_type', $discount_type );
@ -454,9 +482,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set amount.
* @since 3.0.0
* @param float $amount
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param float $amount Amount.
*/
public function set_amount( $amount ) {
$amount = wc_format_decimal( $amount );
@ -478,9 +506,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set expiration date.
*
* @since 3.0.0
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_expires( $date ) {
$this->set_date_prop( 'date_expires', $date );
@ -488,9 +516,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set date_created
*
* @since 3.0.0
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_created( $date ) {
$this->set_date_prop( 'date_created', $date );
@ -498,9 +526,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set date_modified
*
* @since 3.0.0
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_modified( $date ) {
$this->set_date_prop( 'date_modified', $date );
@ -508,9 +536,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set how many times this coupon has been used.
* @since 3.0.0
* @param int $usage_count
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param int $usage_count Usage count.
*/
public function set_usage_count( $usage_count ) {
$this->set_prop( 'usage_count', absint( $usage_count ) );
@ -518,9 +546,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set if this coupon can only be used once.
* @since 3.0.0
* @param bool $is_individual_use
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param bool $is_individual_use If is for individual use.
*/
public function set_individual_use( $is_individual_use ) {
$this->set_prop( 'individual_use', (bool) $is_individual_use );
@ -528,9 +556,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the product IDs this coupon can be used with.
* @since 3.0.0
* @param array $product_ids
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param array $product_ids Products IDs.
*/
public function set_product_ids( $product_ids ) {
$this->set_prop( 'product_ids', array_filter( wp_parse_id_list( (array) $product_ids ) ) );
@ -538,9 +566,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the product IDs this coupon cannot be used with.
* @since 3.0.0
* @param array $excluded_product_ids
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param array $excluded_product_ids Exclude product IDs.
*/
public function set_excluded_product_ids( $excluded_product_ids ) {
$this->set_prop( 'excluded_product_ids', array_filter( wp_parse_id_list( (array) $excluded_product_ids ) ) );
@ -548,9 +576,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the amount of times this coupon can be used.
* @since 3.0.0
* @param int $usage_limit
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param int $usage_limit Usage limit.
*/
public function set_usage_limit( $usage_limit ) {
$this->set_prop( 'usage_limit', absint( $usage_limit ) );
@ -558,9 +586,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the amount of times this coupon can be used per user.
* @since 3.0.0
* @param int $usage_limit
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param int $usage_limit Usage limit.
*/
public function set_usage_limit_per_user( $usage_limit ) {
$this->set_prop( 'usage_limit_per_user', absint( $usage_limit ) );
@ -568,9 +596,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set usage limit to x number of items.
* @since 3.0.0
* @param int|null $limit_usage_to_x_items
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param int|null $limit_usage_to_x_items Limit usage to X items.
*/
public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) {
$this->set_prop( 'limit_usage_to_x_items', is_null( $limit_usage_to_x_items ) ? null : absint( $limit_usage_to_x_items ) );
@ -578,9 +606,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set if this coupon enables free shipping or not.
* @since 3.0.0
* @param bool $free_shipping
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param bool $free_shipping If grant free shipping.
*/
public function set_free_shipping( $free_shipping ) {
$this->set_prop( 'free_shipping', (bool) $free_shipping );
@ -588,9 +616,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the product category IDs this coupon can be used with.
* @since 3.0.0
* @param array $product_categories
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param array $product_categories List of product categories.
*/
public function set_product_categories( $product_categories ) {
$this->set_prop( 'product_categories', array_filter( wp_parse_id_list( (array) $product_categories ) ) );
@ -598,9 +626,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the product category IDs this coupon cannot be used with.
* @since 3.0.0
* @param array $excluded_product_categories
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param array $excluded_product_categories List of excluded product categories.
*/
public function set_excluded_product_categories( $excluded_product_categories ) {
$this->set_prop( 'excluded_product_categories', array_filter( wp_parse_id_list( (array) $excluded_product_categories ) ) );
@ -608,9 +636,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set if this coupon should excluded sale items or not.
* @since 3.0.0
* @param bool $exclude_sale_items
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param bool $exclude_sale_items If should exclude sale items.
*/
public function set_exclude_sale_items( $exclude_sale_items ) {
$this->set_prop( 'exclude_sale_items', (bool) $exclude_sale_items );
@ -618,9 +646,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the minimum spend amount.
* @since 3.0.0
* @param float $amount
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param float $amount Minium amount.
*/
public function set_minimum_amount( $amount ) {
$this->set_prop( 'minimum_amount', wc_format_decimal( $amount ) );
@ -628,9 +656,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set the maximum spend amount.
* @since 3.0.0
* @param float $amount
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param float $amount Maximum amount.
*/
public function set_maximum_amount( $amount ) {
$this->set_prop( 'maximum_amount', wc_format_decimal( $amount ) );
@ -638,9 +666,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set email restrictions.
* @since 3.0.0
* @param array $emails
* @throws WC_Data_Exception
*
* @since 3.0.0
* @param array $emails List of emails.
*/
public function set_email_restrictions( $emails = array() ) {
$emails = array_filter( array_map( 'sanitize_email', array_map( 'strtolower', (array) $emails ) ) );
@ -654,9 +682,9 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set which users have used this coupon.
*
* @since 3.0.0
* @param array $used_by
* @throws WC_Data_Exception
* @param array $used_by List of user IDs.
*/
public function set_used_by( $used_by ) {
$this->set_prop( 'used_by', array_filter( $used_by ) );
@ -664,6 +692,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set coupon virtual state.
*
* @param boolean $virtual Whether it is virtual or not.
* @since 3.2.0
*/
@ -679,42 +708,43 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Developers can programmatically return coupons. This function will read those values into our WC_Coupon class.
* @since 3.0.0
* @param string $code Coupon code
* @param array $coupon Array of coupon properties
*
* @since 3.0.0
* @param string $code Coupon code.
* @param array $coupon Array of coupon properties.
*/
public function read_manual_coupon( $code, $coupon ) {
foreach ( $coupon as $key => $value ) {
switch ( $key ) {
case 'excluded_product_ids' :
case 'exclude_product_ids' :
case 'excluded_product_ids':
case 'exclude_product_ids':
if ( ! is_array( $coupon[ $key ] ) ) {
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
$coupon['excluded_product_ids'] = wc_string_to_array( $value );
}
break;
case 'exclude_product_categories' :
case 'excluded_product_categories' :
case 'exclude_product_categories':
case 'excluded_product_categories':
if ( ! is_array( $coupon[ $key ] ) ) {
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
$coupon['excluded_product_categories'] = wc_string_to_array( $value );
}
break;
case 'product_ids' :
case 'product_ids':
if ( ! is_array( $coupon[ $key ] ) ) {
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
$coupon[ $key ] = wc_string_to_array( $value );
}
break;
case 'individual_use' :
case 'free_shipping' :
case 'exclude_sale_items' :
case 'individual_use':
case 'free_shipping':
case 'exclude_sale_items':
if ( ! is_bool( $coupon[ $key ] ) ) {
wc_doing_it_wrong( $key, $key . ' should be true or false instead of yes or no.', '3.0' );
$coupon[ $key ] = wc_string_to_bool( $value );
}
break;
case 'expiry_date' :
case 'expiry_date':
$coupon['date_expires'] = $value;
break;
}
@ -728,7 +758,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Increase usage count for current coupon.
*
* @param string $used_by Either user ID or billing email
* @param string $used_by Either user ID or billing email.
*/
public function increase_usage_count( $used_by = '' ) {
if ( $this->get_id() && $this->data_store ) {
@ -745,7 +775,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Decrease usage count for current coupon.
*
* @param string $used_by Either user ID or billing email
* @param string $used_by Either user ID or billing email.
*/
public function decrease_usage_count( $used_by = '' ) {
if ( $this->get_id() && $this->get_usage_count() > 0 && $this->data_store ) {
@ -760,10 +790,10 @@ class WC_Coupon extends WC_Legacy_Coupon {
}
/*
|--------------------------------------------------------------------------
| Validation & Error Handling
|--------------------------------------------------------------------------
*/
|--------------------------------------------------------------------------
| Validation & Error Handling
|--------------------------------------------------------------------------
*/
/**
* Returns the error_message string.
@ -779,8 +809,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Check if a coupon is valid for the cart.
*
* @deprecated 3.2.0 In favor of WC_Discounts->is_coupon_valid.
* @throws Exception
* @return bool Validity.
* @return bool
*/
public function is_valid() {
$discounts = new WC_Discounts( WC()->cart );
@ -806,9 +835,8 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Check if a coupon is valid for a product.
*
* @param WC_Product $product
* @param array $values
*
* @param WC_Product $product Product instance.
* @param array $values Values.
* @return bool
*/
public function is_valid_for_product( $product, $values = array() ) {
@ -820,32 +848,32 @@ class WC_Coupon extends WC_Legacy_Coupon {
$product_cats = wc_get_product_cat_ids( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() );
$product_ids = array( $product->get_id(), $product->get_parent_id() );
// Specific products get the discount
if ( sizeof( $this->get_product_ids() ) && sizeof( array_intersect( $product_ids, $this->get_product_ids() ) ) ) {
// Specific products get the discount.
if ( count( $this->get_product_ids() ) && count( array_intersect( $product_ids, $this->get_product_ids() ) ) ) {
$valid = true;
}
// Category discounts
if ( sizeof( $this->get_product_categories() ) && sizeof( array_intersect( $product_cats, $this->get_product_categories() ) ) ) {
// Category discounts.
if ( count( $this->get_product_categories() ) && count( array_intersect( $product_cats, $this->get_product_categories() ) ) ) {
$valid = true;
}
// No product ids - all items discounted
if ( ! sizeof( $this->get_product_ids() ) && ! sizeof( $this->get_product_categories() ) ) {
// No product ids - all items discounted.
if ( ! count( $this->get_product_ids() ) && ! count( $this->get_product_categories() ) ) {
$valid = true;
}
// Specific product IDs excluded from the discount
if ( sizeof( $this->get_excluded_product_ids() ) && sizeof( array_intersect( $product_ids, $this->get_excluded_product_ids() ) ) ) {
// Specific product IDs excluded from the discount.
if ( count( $this->get_excluded_product_ids() ) && count( array_intersect( $product_ids, $this->get_excluded_product_ids() ) ) ) {
$valid = false;
}
// Specific categories excluded from the discount
if ( sizeof( $this->get_excluded_product_categories() ) && sizeof( array_intersect( $product_cats, $this->get_excluded_product_categories() ) ) ) {
// Specific categories excluded from the discount.
if ( count( $this->get_excluded_product_categories() ) && count( array_intersect( $product_cats, $this->get_excluded_product_categories() ) ) ) {
$valid = false;
}
// Sale Items excluded from discount
// Sale Items excluded from discount.
if ( $this->get_exclude_sale_items() && $product->is_on_sale() ) {
$valid = false;
}
@ -876,20 +904,20 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Map one of the WC_Coupon message codes to a message string.
*
* @param integer $msg_code
* @return string| Message/error string
* @param integer $msg_code Message code.
* @return string Message/error string.
*/
public function get_coupon_message( $msg_code ) {
switch ( $msg_code ) {
case self::WC_COUPON_SUCCESS :
case self::WC_COUPON_SUCCESS:
$msg = __( 'Coupon code applied successfully.', 'woocommerce' );
break;
case self::WC_COUPON_REMOVED :
break;
case self::WC_COUPON_REMOVED:
$msg = __( 'Coupon code removed successfully.', 'woocommerce' );
break;
break;
default:
$msg = '';
break;
break;
}
return apply_filters( 'woocommerce_coupon_message', $msg, $msg_code, $this );
}
@ -904,49 +932,49 @@ class WC_Coupon extends WC_Legacy_Coupon {
switch ( $err_code ) {
case self::E_WC_COUPON_INVALID_FILTERED:
$err = __( 'Coupon is not valid.', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_NOT_EXIST:
/* translators: %s: coupon code */
$err = sprintf( __( 'Coupon "%s" does not exist!', 'woocommerce' ), $this->get_code() );
break;
break;
case self::E_WC_COUPON_INVALID_REMOVED:
/* translators: %s: coupon code */
$err = sprintf( __( 'Sorry, it seems the coupon "%s" is invalid - it has now been removed from your order.', 'woocommerce' ), $this->get_code() );
break;
break;
case self::E_WC_COUPON_NOT_YOURS_REMOVED:
/* translators: %s: coupon code */
$err = sprintf( __( 'Sorry, it seems the coupon "%s" is not yours - it has now been removed from your order.', 'woocommerce' ), $this->get_code() );
break;
break;
case self::E_WC_COUPON_ALREADY_APPLIED:
$err = __( 'Coupon code already applied!', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY:
/* translators: %s: coupon code */
$err = sprintf( __( 'Sorry, coupon "%s" has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ), $this->get_code() );
break;
break;
case self::E_WC_COUPON_USAGE_LIMIT_REACHED:
$err = __( 'Coupon usage limit has been reached.', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_EXPIRED:
$err = __( 'This coupon has expired.', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET:
/* translators: %s: coupon minimum amount */
$err = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $this->get_minimum_amount() ) );
break;
break;
case self::E_WC_COUPON_MAX_SPEND_LIMIT_MET:
/* translators: %s: coupon maximum amount */
$err = sprintf( __( 'The maximum spend for this coupon is %s.', 'woocommerce' ), wc_price( $this->get_maximum_amount() ) );
break;
break;
case self::E_WC_COUPON_NOT_APPLICABLE:
$err = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_EXCLUDED_PRODUCTS:
// Store excluded products that are in cart in $products
// Store excluded products that are in cart in $products.
$products = array();
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( in_array( $cart_item['product_id'], $this->get_excluded_product_ids() ) || in_array( $cart_item['variation_id'], $this->get_excluded_product_ids() ) || in_array( $cart_item['data']->get_parent_id(), $this->get_excluded_product_ids() ) ) {
if ( in_array( intval( $cart_item['product_id'] ), $this->get_excluded_product_ids(), true ) || in_array( intval( $cart_item['variation_id'] ), $this->get_excluded_product_ids(), true ) || in_array( intval( $cart_item['data']->get_parent_id() ), $this->get_excluded_product_ids(), true ) ) {
$products[] = $cart_item['data']->get_name();
}
}
@ -956,16 +984,16 @@ class WC_Coupon extends WC_Legacy_Coupon {
$err = sprintf( __( 'Sorry, this coupon is not applicable to the products: %s.', 'woocommerce' ), implode( ', ', $products ) );
break;
case self::E_WC_COUPON_EXCLUDED_CATEGORIES:
// Store excluded categories that are in cart in $categories
// Store excluded categories that are in cart in $categories.
$categories = array();
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
$intersect = array_intersect( $product_cats, $this->get_excluded_product_categories() );
if ( sizeof( $intersect = array_intersect( $product_cats, $this->get_excluded_product_categories() ) ) > 0 ) {
if ( count( $intersect ) > 0 ) {
foreach ( $intersect as $cat_id ) {
$cat = get_term( $cat_id, 'product_cat' );
$cat = get_term( $cat_id, 'product_cat' );
$categories[] = $cat->name;
}
}
@ -977,10 +1005,10 @@ class WC_Coupon extends WC_Legacy_Coupon {
break;
case self::E_WC_COUPON_NOT_VALID_SALE_ITEMS:
$err = __( 'Sorry, this coupon is not valid for sale items.', 'woocommerce' );
break;
break;
default:
$err = '';
break;
break;
}
return apply_filters( 'woocommerce_coupon_error', $err, $err_code, $this );
}
@ -990,22 +1018,22 @@ class WC_Coupon extends WC_Legacy_Coupon {
* No coupon instance will be available where a coupon does not exist,
* so this static method exists.
*
* @param int $err_code Error code
* @return string| Error string
* @param int $err_code Error code.
* @return string Error string.
*/
public static function get_generic_coupon_error( $err_code ) {
switch ( $err_code ) {
case self::E_WC_COUPON_NOT_EXIST:
$err = __( 'Coupon does not exist!', 'woocommerce' );
break;
break;
case self::E_WC_COUPON_PLEASE_ENTER:
$err = __( 'Please enter a coupon code.', 'woocommerce' );
break;
break;
default:
$err = '';
break;
break;
}
// When using this static method, there is no $this to pass to filter
// When using this static method, there is no $this to pass to filter.
return apply_filters( 'woocommerce_coupon_error', $err, $err_code, null );
}
}