Hello to all, welcome again on therichpost.com. In this post, I will tell you, WordPress – Custom Rest Api for Woocommerce product category filter.
Post Working:
In this post, I am making WordPress rest api, with I am getting woocommerce product by category slug.
Here is the code snippet and you need to add this into your theme’s functions.php file:
add_action( 'rest_api_init', function () {
register_rest_route( 'productfiltercat/v1', '/product/category/(?P<slug>[^/]+)', array(
'methods' => 'GET',
'callback' => 'therich_func',
) );
} );
function therich_func( $data ) {
$p = wc_get_products(array('status' => 'publish', 'category' => $data['slug']));
$products = array();
foreach ($p as $product) {
$products[] = $product->get_data();
}
return new WP_REST_Response($products, 200);
}
Here is my Rest API path:
http://localhost/wordpress/wp-json/productfiltercat/v1/product/category/category-1
If you have any kind of query then please do comment below.
Jassa
Thanks

Leave a Reply
You must be logged in to post a comment.