Reputation: 762
I want filter products by meta_data in woocommerce rest api. I tried this, but not working:
$data = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'package_type',
'value' => $filters['ad_type'],
'compare' => '='
),
array(
'key' => 'package_location',
'value' => $filters['ad_location'],
'compare' => '='
),
array(
'key' => 'package_price_range',
'value' => $filters['ad_price_range'],
'compare' => '='
)
)
);
$_results = $this->woocommerce->get('products', $data);
It seems the $data filter is not prevail, it will get back the all product without filter. So the questions is, how can i use meta filters in api request?
Upvotes: 1
Views: 2062
Reputation: 762
I found a solution. Extend/override woocommerce/packages/woocommerce-rest-api/src/Controllers/Version3/class-wc-rest-products-controller.php controller,
put the following:
if ( ! empty( $request['custom_filter'] && ! empty( $request['meta_key'] ) ) ) {
$args['meta_query'] = $this->add_meta_query( $args, array(
'key' => $request['meta_key'],
'value' => $request['meta_value'],
'compare' => '=',
) );
}
and tada.wav you can filter by meta_query!
Upvotes: 3