wc_get_coupon_id_by_code

This commit is contained in:
Mike Jolley 2016-08-30 11:43:53 +01:00
parent b0672a000a
commit df35845d4d
6 changed files with 44 additions and 90 deletions

View File

@ -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' ) );
}

View File

@ -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 ) );
}

View File

@ -226,17 +226,9 @@ class WC_API_Coupons extends WC_API_Resource {
}
$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 );
}
@ -341,18 +333,9 @@ class WC_API_Coupons extends WC_API_Resource {
global $wpdb;
$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 );
}

View File

@ -226,17 +226,9 @@ class WC_API_Coupons extends WC_API_Resource {
}
$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 );
}
@ -341,18 +333,9 @@ class WC_API_Coupons extends WC_API_Resource {
global $wpdb;
$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 );
}

View File

@ -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.
*

View File

@ -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 );
}