How to add filter in woocommerce API

I would like to use API of woocommerce to filter products, for example: wc-api/v2/products
I can filter by update function query_products in class-wc-api-products.php as below:

private function query_products( $args ) {

// Set base query arguments
$from_price = (!empty($args[‘from_price’])) ? $args[‘from_price’] : 0;
$to_price = (!empty($args[‘to_price’])) ? $args[‘to_price’] : 9999999999;
$query_args = array(
‘fields’ => ‘ids’,
‘post_type’ => ‘product’,

// 2018-03-29 Tuan add begin
‘product_cat’ => $args[‘product_cat’],
‘pa_size-infants-clothing’ => $args[‘pa_size-infants-clothing’],
‘pa_size-kids-clothing’ => $args[‘pa_size-kids-clothing’],
‘pa_size-toddlers-clothing’ => $args[‘pa_size-toddlers-clothing’],
‘pa_size-men-clothing’ => $args[‘pa_size-men-clothing’],
‘pa_size-men-pant’ => $args[‘pa_size-men-pant’],
‘pa_size-women-clothing’ => $args[‘pa_size-women-clothing’],
‘pa_size-kid-shoes’ => $args[‘”pa_size-kid-shoes’],
‘pa_size-men-shoes’ => $args[‘pa_size-men-shoes’],
‘pa_size-women-shoes’ => $args[‘pa_size-women-shoes’],
‘pa_watch-styles’ => $args[‘pa_watch-styles’],
‘pa_gender’ => $args[‘pa_gender’],
‘from_price’ => $args[‘from_price’],
‘to_price’ => $args[‘to_price’],
‘yith_product_brand’ => $args[‘yith_product_brand’],
‘product_color’ => $args[‘product_color’],
‘product_visibility’ => $args[‘product_visibility’],
‘product_tag’ => $args[‘product_tag’],
‘pa_colors-patterns’ => $args[‘pa_colors-patterns’],
‘pa_editors-picks’ => $args[‘pa_editors-picks’],
‘pa_items-with-tags’ => $args[‘pa_items-with-tags’],
‘pa_on-sale-now’ => $args[‘pa_on-sale-now’],
‘pa_hide-sold-items’ => $args[‘pa_hide-sold-items’],
‘pa_est-retail’ => $args[‘pa_est-retail’],
‘pa_mens-inseam’ => $args[‘pa_mens-inseam’],
‘pa_mens-waist’ => $args[‘pa_mens-waist’],
// 2018-03-29 Tuan add end

‘post_status’ => ‘publish’,

// Require from_price and to_price
‘meta_query’ => array(
array(
‘key’ => ‘_price’,
‘value’ => array($from_price, $to_price),
‘compare’ => ‘BETWEEN’,
‘type’ => ‘NUMERIC’
),
),
);

if ( ! empty( $args[‘type’] ) ) {

$types = explode( ‘,’, $args[‘type’] );

$query_args[‘tax_query’] = array(
array(
‘taxonomy’ => ‘product_type’,
‘field’ => ‘slug’,
‘terms’ => $types,
),
);

unset( $args[‘type’] );
}

// Filter products by category
if ( ! empty( $args[‘category’] ) ) {
$query_args[‘product_cat’] = $args[‘category’];
}

// Filter by specific sku
if ( ! empty( $args[‘sku’] ) ) {
if ( ! is_array( $query_args[‘meta_query’] ) ) {
$query_args[‘meta_query’] = array();
}

$query_args[‘meta_query’][] = array(
‘key’ => ‘_sku’,
‘value’ => $args[‘sku’],
‘compare’ => ‘=’,
);

$query_args[‘post_type’] = array( ‘product’, ‘product_variation’ );
}

$query_args = $this->merge_query_args( $query_args, $args );

return new WP_Query( $query_args );
}

But If I do it I will not update woocommerce later. Could you please help me advise?

Thank you

Read more here:: How to add filter in woocommerce API

Leave a Reply

Your email address will not be published. Required fields are marked *