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);
}
http://localhost/wordpress/wp-json/productfiltercat/v1/product/category/category-1
#woocommerce #woocommerce hooks #wordpress #wordpress hooks #wordpress tricks #wp tricks