From df35845d4d20ef2e9b22780dfb35db8531f8d759 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 30 Aug 2016 11:43:53 +0100 Subject: [PATCH] wc_get_coupon_id_by_code --- .../class-wc-meta-box-coupon-data.php | 11 ++----- .../api/class-wc-rest-coupons-controller.php | 13 ++------- .../api/legacy/v2/class-wc-api-coupons.php | 29 ++++--------------- .../api/legacy/v3/class-wc-api-coupons.php | 29 ++++--------------- includes/class-wc-coupon.php | 23 +-------------- includes/wc-coupon-functions.php | 29 +++++++++++++++++-- 6 files changed, 44 insertions(+), 90 deletions(-) diff --git a/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php b/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php index 38fa953e628..68fd7eb6a6c 100644 --- a/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php +++ b/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php @@ -258,16 +258,9 @@ class WC_Meta_Box_Coupon_Data { // Check for dupe coupons $coupon_code = apply_filters( 'woocommerce_coupon_code', $post->post_title ); - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = %s - AND $wpdb->posts.ID != %d - ", $coupon_code, $post_id ) ); + $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $post_id ); - if ( $coupon_found ) { + if ( $id_from_code ) { WC_Admin_Meta_Boxes::add_error( __( 'Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce' ) ); } diff --git a/includes/api/class-wc-rest-coupons-controller.php b/includes/api/class-wc-rest-coupons-controller.php index c5a464c2d5d..f81a5584a94 100644 --- a/includes/api/class-wc-rest-coupons-controller.php +++ b/includes/api/class-wc-rest-coupons-controller.php @@ -228,18 +228,9 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller { case 'code' : $coupon_code = apply_filters( 'woocommerce_coupon_code', $value ); $id = $coupon->get_id() ? $coupon->get_id() : 0; + $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id ); - // Check for duplicate coupon codes. - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = %s - AND $wpdb->posts.ID != %s - ", $coupon_code, $id ) ); - - if ( $coupon_found ) { + if ( $id_from_code ) { return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) ); } diff --git a/includes/api/legacy/v2/class-wc-api-coupons.php b/includes/api/legacy/v2/class-wc-api-coupons.php index 4906f1deb48..860c47c2671 100644 --- a/includes/api/legacy/v2/class-wc-api-coupons.php +++ b/includes/api/legacy/v2/class-wc-api-coupons.php @@ -225,18 +225,10 @@ class WC_API_Coupons extends WC_API_Resource { throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 ); } - $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $id_from_code = wc_get_coupon_id_by_code( $coupon_code ); - // Check for duplicate coupon codes - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = '%s' - ", $coupon_code ) ); - - if ( $coupon_found ) { + if ( $id_from_code ) { throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); } @@ -340,19 +332,10 @@ class WC_API_Coupons extends WC_API_Resource { if ( isset( $data['code'] ) ) { global $wpdb; - $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id ); - // Check for duplicate coupon codes - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = '%s' - AND $wpdb->posts.ID != %s - ", $coupon_code, $id ) ); - - if ( $coupon_found ) { + if ( $id_from_code ) { throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); } diff --git a/includes/api/legacy/v3/class-wc-api-coupons.php b/includes/api/legacy/v3/class-wc-api-coupons.php index 4906f1deb48..860c47c2671 100644 --- a/includes/api/legacy/v3/class-wc-api-coupons.php +++ b/includes/api/legacy/v3/class-wc-api-coupons.php @@ -225,18 +225,10 @@ class WC_API_Coupons extends WC_API_Resource { throw new WC_API_Exception( 'woocommerce_api_missing_coupon_code', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'code' ), 400 ); } - $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $id_from_code = wc_get_coupon_id_by_code( $coupon_code ); - // Check for duplicate coupon codes - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = '%s' - ", $coupon_code ) ); - - if ( $coupon_found ) { + if ( $id_from_code ) { throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); } @@ -340,19 +332,10 @@ class WC_API_Coupons extends WC_API_Resource { if ( isset( $data['code'] ) ) { global $wpdb; - $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $coupon_code = apply_filters( 'woocommerce_coupon_code', $data['code'] ); + $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id ); - // Check for duplicate coupon codes - $coupon_found = $wpdb->get_var( $wpdb->prepare( " - SELECT $wpdb->posts.ID - FROM $wpdb->posts - WHERE $wpdb->posts.post_type = 'shop_coupon' - AND $wpdb->posts.post_status = 'publish' - AND $wpdb->posts.post_title = '%s' - AND $wpdb->posts.ID != %s - ", $coupon_code, $id ) ); - - if ( $coupon_found ) { + if ( $id_from_code ) { throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 ); } diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index aed491e5017..74f4484f500 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -105,7 +105,7 @@ class WC_Coupon extends WC_Legacy_Coupon { $this->read( $data ); } elseif ( ! empty( $data ) ) { $this->set_code( $data ); - $this->read( absint( self::get_coupon_id_from_code( $data ) ) ); + $this->read( wc_get_coupon_id_by_code( $data ) ); } } @@ -333,27 +333,6 @@ class WC_Coupon extends WC_Legacy_Coupon { return $this->_data['used_by']; } - /** - * Get a coupon ID from it's code. - * @since 2.5.0 woocommerce_coupon_code_query was removed in favour of woocommerce_get_coupon_id_from_code filter on the return. wp_cache was also implemented. - * @param string $code - * @return int - */ - private function get_coupon_id_from_code( $code ) { - global $wpdb; - - $coupon_id = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' ); - - if ( false === $coupon_id ) { - $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $this->get_code() ); - - if ( $coupon_id = apply_filters( 'woocommerce_get_coupon_id_from_code', $wpdb->get_var( $sql ), $this->get_code() ) ) { - wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $coupon_id, 'coupons' ); - } - } - return absint( $coupon_id ); - } - /** * Get discount amount for a cart item. * diff --git a/includes/wc-coupon-functions.php b/includes/wc-coupon-functions.php index dd664849434..740d0277488 100644 --- a/includes/wc-coupon-functions.php +++ b/includes/wc-coupon-functions.php @@ -75,9 +75,7 @@ function wc_coupons_enabled() { * Get coupon code by ID. * * @since 2.7.0 - * * @param int $id Coupon ID. - * * @return string */ function wc_get_coupon_code_by_id( $id ) { @@ -93,3 +91,30 @@ function wc_get_coupon_code_by_id( $id ) { return (string) $code; } + +/** + * Get coupon code by ID. + * + * @since 2.7.0 + * @param string $code + * @param int $exclude Used to exclude an ID from the check if you're checking existance. + * @return int + */ +function wc_get_coupon_id_by_code( $code, $exclude = 0 ) { + global $wpdb; + + $ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' ); + + if ( false === $ids ) { + $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC;", $code ); + $ids = $wpdb->get_col( $sql ); + + if ( $ids ) { + wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $ids, 'coupons' ); + } + } + + $ids = array_diff( array_filter( array_map( 'absint', (array) $ids ) ), array( $exclude ) ); + + return apply_filters( 'woocommerce_get_coupon_id_from_code', absint( current( $ids ) ), $code, $exclude ); +}