White creating some unique REST API endpoints that get products with some special requirements, e.g. one endpoint for the products featured. I have tried using the function wc_get_products
like this.
add_action('rest_api_init', 'my_custom_featured_product_endpoint');
function my_custom_featured_product_endpoint() {
register_rest_route('custom-endpoints/v1', '/products/featured', array(
'methods' => 'GET',
'callback' => 'my_custom_featured_product_callback',
));
}
function my_custom_featured_product_callback() {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'tax_query' => $tax_query,
'meta_query' => $meta_query,
);
$result = wc_get_products($args);
return rest_ensure_response($result);
}
I'm getting Just a few empty arrays. the out have same old items get_posts
to replace wc_get_products
but don't have format with properties like 'price', 'images'
I also tested the query by changing the callback function but result was same
function my_custom_featured_product_callback() {
$result = wc_get_product(99);//Yes there is a product with ID 99
return rest_ensure_response($result);
}
Is there any alternatives for wc_get_products
to use for custom REST API endpoints?