WC_API_Coupons::get_coupon_by_code() public WC 2.1
Get the coupon for the given code
{} It's a method of the class: WC_API_Coupons{}
No Hooks.
Return
Int|WP_Error.
Usage
$WC_API_Coupons = new WC_API_Coupons(); $WC_API_Coupons->get_coupon_by_code( $code, $fields );
- $code(string) (required)
- the coupon code
- $fields(string)
- fields to include in response
Changelog
Since 2.1 | Introduced. |
Code of WC_API_Coupons::get_coupon_by_code() WC API Coupons::get coupon by code WC 5.0.0
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' 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 ) );
}
return $this->get_coupon( $id, $fields );
}