[2.5] When getting coupon by code used twice, latest should be queried
Closes #10487
This commit is contained in:
parent
f926f8e9c9
commit
f1eefa902c
|
@ -186,7 +186,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
global $wpdb;
|
||||
|
||||
try {
|
||||
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $code ) );
|
||||
$id = $wpdb->get_var( $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;", $code ) );
|
||||
|
||||
if ( is_null( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
|
||||
|
|
|
@ -166,7 +166,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
public function get_coupon_by_code( $code, $fields = null ) {
|
||||
global $wpdb;
|
||||
|
||||
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $code ) );
|
||||
$id = $wpdb->get_var( $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;", $code ) );
|
||||
|
||||
if ( is_null( $id ) )
|
||||
return new WP_Error( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
|
|
|
@ -186,7 +186,7 @@ class WC_API_Coupons extends WC_API_Resource {
|
|||
global $wpdb;
|
||||
|
||||
try {
|
||||
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $code ) );
|
||||
$id = $wpdb->get_var( $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;", $code ) );
|
||||
|
||||
if ( is_null( $id ) ) {
|
||||
throw new WC_API_Exception( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), 404 );
|
||||
|
|
|
@ -155,7 +155,7 @@ class WC_Coupon {
|
|||
$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'", $this->code );
|
||||
$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->code );
|
||||
$coupon_id = apply_filters( 'woocommerce_get_coupon_id_from_code', $wpdb->get_var( $sql ), $this->code );
|
||||
wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $coupon_id, 'coupons' );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue