admin管理员组文章数量:1389758
I'm trying to create some custom REST API endpoints which get products with some special conditions, for example, one endpoint for featured products.
I tried to use the wc_get_products
function 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);
}
The result is just some empty arrays. I can get those products with old fashion get_posts
to replace wc_get_products
but the output format doesn't have some properties like 'price', 'images' ...
So are there any alternatives for wc_get_products
to use for custom REST API endpoints or are there any ways to make it work?
P/S: I tested the query by change the callback function like so:
function my_custom_featured_product_callback() {
$result = wc_get_product(99);//Yes there is a product with ID 99
return rest_ensure_response($result);
}
The result stays the same, just an empty array. So I think the issue must lie with the wc_get_products
and wc_get_product
functions. Maybe the rest_api_init
is not the proper hook for those functions?
I'm trying to create some custom REST API endpoints which get products with some special conditions, for example, one endpoint for featured products.
I tried to use the wc_get_products
function 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);
}
The result is just some empty arrays. I can get those products with old fashion get_posts
to replace wc_get_products
but the output format doesn't have some properties like 'price', 'images' ...
So are there any alternatives for wc_get_products
to use for custom REST API endpoints or are there any ways to make it work?
P/S: I tested the query by change the callback function like so:
function my_custom_featured_product_callback() {
$result = wc_get_product(99);//Yes there is a product with ID 99
return rest_ensure_response($result);
}
The result stays the same, just an empty array. So I think the issue must lie with the wc_get_products
and wc_get_product
functions. Maybe the rest_api_init
is not the proper hook for those functions?
3 Answers
Reset to default 3you missed something, when you get a product using wc_get_product it returns to you an abstract object, so if you need to get product do this
$product = wc_get_product($product_id);
return $product->get_data();
also you can use all the other functionalities too, such as:
$product->get_status();
$product->get_gallery_image_ids();
...
I faced a similar behavior where $wc_get_product
always returned empty objects from a custom REST endpoint when I was checking the response with a console.log on the front end side.
It was because I was returning the PHP Object directly in the response, I solved the issue by converting the Product Object to an array with the help of the thread bellow :
convert-php-object-to-associative-array
After that it worked as expected, hopefully it'll be the same for you!
Add (array)
cast before wc_get_products($args)
like this:
$result = (array) wc_get_products($args);
If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. and you can access to product data by *data
property.
本文标签: queryWooCommerce Can39t use wcgetproducts for custom REST API endpoints
版权声明:本文标题:query - WooCommerce: Can't use wc_get_products for custom REST API endpoints 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744669505a2618740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$meta_query
and$tax_query
ending up as anyway? – Jacob Peattie Commented Dec 14, 2017 at 10:27rest_api_init
hook, the result is just some empty arrays. – David Lee Commented Dec 14, 2017 at 10:43WC()->query
. In the REST API WooCommerce won't have run a query. – Jacob Peattie Commented Dec 14, 2017 at 10:45rest_ensure_response()
. You should just be returning the$result
. If you need the posts to match the normal format of returned posts, they need to be prepared. – Jacob Peattie Commented Dec 14, 2017 at 12:57