i am developing mobile application to connect WooCommerce wordpress V3 rest API in iOS and Android.
i am getting the product list from Rest API
"wp-json/wc/v3/products"
i am able to filter product using
"wp-json/wc/v3/productscategory=241&attribute=pa_flavor&attribute_term=301"
and filter multiple attribute term using
"wp-json/wc/v3/products?attribute=pa_flavor&attribute_term=301,302,305".
i need to filter product with multiple attribute(like pa_flavor,pa_size,pa_color... etc.) in rest API. how can do that?
Create Custom REST API for Filter Products by multiple attributes
// Create Custom REST API for Filter
add_action('rest_api_init', 'wp_rest_filterproducts_endpoints');
function wp_rest_filterproducts_endpoints($request) {
register_rest_route('wp/v3', 'filter/products', array(
'methods' => 'GET',
'callback' => 'wp_rest_filterproducts_endpoint_handler',
));
}
function wp_rest_filterproducts_endpoint_handler($request = null) {
$output = array();
$params = $request->get_params();
$category = $params['category'];
$filters = $params['filter'];
$per_page = $params['per_page'];
$offset = $params['offset'];
$order = $params['order'];
$orderby = $params['orderby'];
// Use default arguments.
$args = [
'post_type' => 'product',
'posts_per_page' => 10,
'post_status' => 'publish',
'paged' => 1,
];
// Posts per page.
if ( ! empty( $per_page ) ) {
$args['posts_per_page'] = $per_page;
}
// Pagination, starts from 1.
if ( ! empty( $offset ) ) {
$args['paged'] = $offset;
}
// Order condition. ASC/DESC.
if ( ! empty( $order ) ) {
$args['order'] = $order;
}
// Orderby condition. Name/Price.
if ( ! empty( $orderby ) ) {
if ( $orderby === 'price' ) {
$args['orderby'] = 'meta_value_num';
} else {
$args['orderby'] = $orderby;
}
}
// If filter buy category or attributes.
if ( ! empty( $category ) || ! empty( $filters ) ) {
$args['tax_query']['relation'] = 'AND';
// Category filter.
if ( ! empty( $category ) ) {
$args['tax_query'][] = [
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => [ $category ],
];
}
// Attributes filter.
if ( ! empty( $filters ) ) {
foreach ( $filters as $filter_key => $filter_value ) {
if ( $filter_key === 'min_price' || $filter_key === 'max_price' ) {
continue;
}
$args['tax_query'][] = [
'taxonomy' => $filter_key,
'field' => 'term_id',
'terms' => \explode( ',', $filter_value ),
];
}
}
// Min / Max price filter.
if ( isset( $filters['min_price'] ) || isset( $filters['max_price'] ) ) {
$price_request = [];
if ( isset( $filters['min_price'] ) ) {
$price_request['min_price'] = $filters['min_price'];
}
if ( isset( $filters['max_price'] ) ) {
$price_request['max_price'] = $filters['max_price'];
}
$args['meta_query'][] = \wc_get_min_max_price_meta_query( $price_request );
}
}
$the_query = new \WP_Query( $args );
if ( ! $the_query->have_posts() ) {
return $output;
}
while ( $the_query->have_posts() ) {
$the_query->the_post();
$product = wc_get_product( get_the_ID() );
// Product Properties
$wcproduct['id'] = $product->get_id();
$wcproduct['name'] = $product->get_name();
$output[] = $wcproduct;
}
wp_reset_postdata();
return new WP_REST_Response($output, 123);
}
The request:
/wp-json/wp/v3/filter/product/?category=kurtis&filter[pa_color]=XX,X,XXX
For Price:
/wp-json/wp/v3/filter/product/?filter[min_price]=500&filter[max_price]=1000&filter[pa_color]=XX
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With