From 9619c041cda4075ad7757e497ba35c58eec3de48 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 30 Mar 2016 18:42:51 -0300 Subject: [PATCH] Allow filter coupons by code --- .../api/class-wc-rest-coupons-controller.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/includes/api/class-wc-rest-coupons-controller.php b/includes/api/class-wc-rest-coupons-controller.php index 8b34c54bcfd..a172f0b2802 100644 --- a/includes/api/class-wc-rest-coupons-controller.php +++ b/includes/api/class-wc-rest-coupons-controller.php @@ -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; + } }