Allow filter coupons by code

This commit is contained in:
Claudio Sanches 2016-03-30 18:42:51 -03:00
parent 1722f95a95
commit 9619c041cd
1 changed files with 44 additions and 0 deletions

View File

@ -43,6 +43,13 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
*/
protected $post_type = 'shop_coupon';
/**
* Order refunds actions.
*/
public function __construct() {
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
}
/**
* Register the routes for coupons.
*/
@ -97,6 +104,25 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
) );
}
/**
* Query args.
*
* @param array $args
* @param WP_REST_Request $request
* @return array
*/
public function query_args( $args, $request ) {
global $wpdb;
if ( ! empty( $request['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'", $request['code'] ) );
$args['post__in'] = array( $id );
}
return $args;
}
/**
* Prepare a single coupon output for response.
*
@ -515,4 +541,22 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
return $this->add_additional_fields_schema( $schema );;
}
/**
* Get the query params for collections of attachments.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['code'] = array(
'description' => __( 'Limit result set to resources with a specific code.', 'woocommerce' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}