wc_get_coupon_id_by_code
This commit is contained in:
parent
b0672a000a
commit
df35845d4d
|
@ -258,16 +258,9 @@ class WC_Meta_Box_Coupon_Data {
|
||||||
|
|
||||||
// Check for dupe coupons
|
// Check for dupe coupons
|
||||||
$coupon_code = apply_filters( 'woocommerce_coupon_code', $post->post_title );
|
$coupon_code = apply_filters( 'woocommerce_coupon_code', $post->post_title );
|
||||||
$coupon_found = $wpdb->get_var( $wpdb->prepare( "
|
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $post_id );
|
||||||
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 ) );
|
|
||||||
|
|
||||||
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' ) );
|
WC_Admin_Meta_Boxes::add_error( __( 'Coupon code already exists - customers will use the latest coupon with this code.', 'woocommerce' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -228,18 +228,9 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
|
||||||
case 'code' :
|
case 'code' :
|
||||||
$coupon_code = apply_filters( 'woocommerce_coupon_code', $value );
|
$coupon_code = apply_filters( 'woocommerce_coupon_code', $value );
|
||||||
$id = $coupon->get_id() ? $coupon->get_id() : 0;
|
$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.
|
if ( $id_from_code ) {
|
||||||
$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 ) {
|
|
||||||
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 );
|
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
|
if ( $id_from_code ) {
|
||||||
$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 ) {
|
|
||||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
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'] ) ) {
|
if ( isset( $data['code'] ) ) {
|
||||||
global $wpdb;
|
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
|
if ( $id_from_code ) {
|
||||||
$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 ) {
|
|
||||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 );
|
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
|
if ( $id_from_code ) {
|
||||||
$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 ) {
|
|
||||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
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'] ) ) {
|
if ( isset( $data['code'] ) ) {
|
||||||
global $wpdb;
|
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
|
if ( $id_from_code ) {
|
||||||
$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 ) {
|
|
||||||
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
throw new WC_API_Exception( 'woocommerce_api_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), 400 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
|
||||||
$this->read( $data );
|
$this->read( $data );
|
||||||
} elseif ( ! empty( $data ) ) {
|
} elseif ( ! empty( $data ) ) {
|
||||||
$this->set_code( $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'];
|
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.
|
* Get discount amount for a cart item.
|
||||||
*
|
*
|
||||||
|
|
|
@ -75,9 +75,7 @@ function wc_coupons_enabled() {
|
||||||
* Get coupon code by ID.
|
* Get coupon code by ID.
|
||||||
*
|
*
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
*
|
|
||||||
* @param int $id Coupon ID.
|
* @param int $id Coupon ID.
|
||||||
*
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function wc_get_coupon_code_by_id( $id ) {
|
function wc_get_coupon_code_by_id( $id ) {
|
||||||
|
@ -93,3 +91,30 @@ function wc_get_coupon_code_by_id( $id ) {
|
||||||
|
|
||||||
return (string) $code;
|
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 );
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue