[REST API] Allow filter by on sale products, closes #12093 (#12105)

This commit is contained in:
Claudio Sanches 2016-10-18 08:40:13 -02:00 committed by Mike Jolley
parent c27fd5c052
commit f218b6cfd9
1 changed files with 12 additions and 0 deletions

View File

@ -215,6 +215,12 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
) ); ) );
} }
// Filter by on sale products.
if ( is_bool( $request['on_sale'] ) ) {
$on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in';
$args[ $on_sale_key ] += wc_get_product_ids_on_sale();
}
// Apply all WP_Query filters again. // Apply all WP_Query filters again.
if ( is_array( $request['filter'] ) ) { if ( is_array( $request['filter'] ) ) {
$args = array_merge( $args, $request['filter'] ); $args = array_merge( $args, $request['filter'] );
@ -2766,6 +2772,12 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
'sanitize_callback' => 'wc_string_to_bool', 'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg', 'validate_callback' => 'rest_validate_request_arg',
); );
$params['on_sale'] = array(
'description' => __( 'Limit result set to products on sale.', 'woocommerce' ),
'type' => 'boolean',
'sanitize_callback' => 'wc_string_to_bool',
'validate_callback' => 'rest_validate_request_arg',
);
$params['min_price'] = array( $params['min_price'] = array(
'description' => __( 'Limit result set to products based on a minimum price.', 'woocommerce' ), 'description' => __( 'Limit result set to products based on a minimum price.', 'woocommerce' ),
'type' => 'string', 'type' => 'string',