Fix variation category restriction and limit to x uses

Fixes #14068
This commit is contained in:
Mike Jolley 2017-04-06 21:51:11 +01:00
parent 8baef77b5c
commit 1b5cad3d78
2 changed files with 9 additions and 8 deletions

View File

@ -37,7 +37,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
'excluded_product_ids' => array(),
'usage_limit' => 0,
'usage_limit_per_user' => 0,
'limit_usage_to_x_items' => 0,
'limit_usage_to_x_items' => null,
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
@ -262,7 +262,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Usage limited to certain amount of items
* @since 3.0.0
* @param string $context
* @return integer
* @return integer|null
*/
public function get_limit_usage_to_x_items( $context = 'view' ) {
return $this->get_prop( 'limit_usage_to_x_items', $context );
@ -388,11 +388,12 @@ class WC_Coupon extends WC_Legacy_Coupon {
// Handle the limit_usage_to_x_items option
if ( ! $this->is_type( array( 'fixed_cart' ) ) ) {
if ( $discounting_amount ) {
if ( ! $this->get_limit_usage_to_x_items() ) {
if ( null === $this->get_limit_usage_to_x_items() ) {
$limit_usage_qty = $cart_item_qty;
} else {
$limit_usage_qty = min( $this->get_limit_usage_to_x_items(), $cart_item_qty );
$this->set_limit_usage_to_x_items( max( 0, $this->get_limit_usage_to_x_items() - $limit_usage_qty ) );
$this->set_limit_usage_to_x_items( max( 0, ( $this->get_limit_usage_to_x_items() - $limit_usage_qty ) ) );
}
if ( $single ) {
$discount = ( $discount * $limit_usage_qty ) / $cart_item_qty;
@ -557,11 +558,11 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set usage limit to x number of items.
* @since 3.0.0
* @param int $limit_usage_to_x_items
* @param int|null $limit_usage_to_x_items
* @throws WC_Data_Exception
*/
public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) {
$this->set_prop( 'limit_usage_to_x_items', absint( $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 ) );
}
/**
@ -1027,7 +1028,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
}
$valid = false;
$product_cats = wc_get_product_cat_ids( $product->get_id() );
$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

View File

@ -105,7 +105,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'excluded_product_ids' => array_filter( (array) explode( ',', get_post_meta( $coupon_id, 'exclude_product_ids', true ) ) ),
'usage_limit' => get_post_meta( $coupon_id, 'usage_limit', true ),
'usage_limit_per_user' => get_post_meta( $coupon_id, 'usage_limit_per_user', true ),
'limit_usage_to_x_items' => get_post_meta( $coupon_id, 'limit_usage_to_x_items', true ),
'limit_usage_to_x_items' => 0 < get_post_meta( $coupon_id, 'limit_usage_to_x_items', true ) ? get_post_meta( $coupon_id, 'limit_usage_to_x_items', true ) : null,
'free_shipping' => 'yes' === get_post_meta( $coupon_id, 'free_shipping', true ),
'product_categories' => array_filter( (array) get_post_meta( $coupon_id, 'product_categories', true ) ),
'excluded_product_categories' => array_filter( (array) get_post_meta( $coupon_id, 'exclude_product_categories', true ) ),