From 1b5cad3d78a0b7b5dd8ec7cc1ad50e8b79290653 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 6 Apr 2017 21:51:11 +0100 Subject: [PATCH] Fix variation category restriction and limit to x uses Fixes #14068 --- includes/class-wc-coupon.php | 15 ++++++++------- .../class-wc-coupon-data-store-cpt.php | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index 2b8dcaea823..d31a546cac1 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -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 diff --git a/includes/data-stores/class-wc-coupon-data-store-cpt.php b/includes/data-stores/class-wc-coupon-data-store-cpt.php index 226c12d8b27..ab38afa44bd 100644 --- a/includes/data-stores/class-wc-coupon-data-store-cpt.php +++ b/includes/data-stores/class-wc-coupon-data-store-cpt.php @@ -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 ) ),