Arno van Oordt
Arno van Oordt

Reputation: 3510

Woocommerce API whitout curl.. Is it possible?

I'm creating a plugin for Wordpress/Woocommerce and I was wondering if there is a simple way to call the Woocommerce API without going through the hassle of the whole REST API thing (curl, authentication, keys, secrets, etc). Since the code runs on the same server as Woocommerce it seems like a much easier and cleaner solution to just call some woocommerce function immediately.

So I'm looking for something like $myProducts = WC->getProducts(); Instead of having to make a Curl request to /wp-json/wc/v2/products

Is there a nice way to do this? Or is the next best option just to start querying the database (since this code is already somewhere in Woocommerce it seems a bit redundant to program it again)?

PS besides getting all the products I have a lot of other calls too so I'm looking for a general approach (the getProducts is just an example).

Upvotes: 3

Views: 734

Answers (1)

Arno van Oordt
Arno van Oordt

Reputation: 3510

Hooray! I finally found a way to do this, thanks to this blog: https://blog.wallacetheme.com/wordpress-theme-rest-api

$request = new WP_REST_Request('GET', '/wc/v2/products');
$result = rest_get_server()->dispatch($request);
return $result->data;

This just bypasses the whole curl request.

Optionally you can use set_query_params and set_body_params for sending optional GET and POST data.

Upvotes: 3

Related Questions