6

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?

6
  • If you're getting an empty array and not an error, then the function is working fine. You're just not getting any posts that match your criteria. What are $meta_query and $tax_query ending up as anyway? Commented Dec 14, 2017 at 10:27
  • Actually the function is working fine in template files and return correct result with properly format. But when I use it with rest_api_init hook, the result is just some empty arrays.
    – David Lee
    Commented Dec 14, 2017 at 10:43
  • I think your problem is your use of WC()->query. In the REST API WooCommerce won't have run a query. Commented Dec 14, 2017 at 10:45
  • As I edited the question, I changed the callback function to test if the problem is the use of query. Please check my question again.
    – David Lee
    Commented Dec 14, 2017 at 11:01
  • Hang on, I'm not sure what you're doing with rest_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. Commented Dec 14, 2017 at 12:57

3 Answers 3

3

you 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();
...
1
  • 1
    THANK YOU! I had been trying to get product data for a long time. Was just missing $product->get_data(). Commented Jun 10, 2021 at 12:03
0

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!

0

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.

1
  • wc_get_products is array itself ! (array of objects).
    – T.Todua
    Commented Apr 16, 2020 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.