WC_API_Coupons::get_coupon_by_code()publicWC 2.1

Get the coupon for the given code

Метод класса: WC_API_Coupons{}

Хуков нет.

Возвращает

int|WP_Error.

Использование

$WC_API_Coupons = new WC_API_Coupons();
$WC_API_Coupons->get_coupon_by_code( $code, $fields );
$code(строка) (обязательный)
the coupon code
$fields(строка)
fields to include in response
По умолчанию: null

Список изменений

С версии 2.1 Введена.

Код WC_API_Coupons::get_coupon_by_code() WC 8.7.0

public function get_coupon_by_code( $code, $fields = null ) {
	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' 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 );
		}

		return $this->get_coupon( $id, $fields );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}