A few years ago I have written a post on “How to create custom endpoint in WooCommerce API“. That was I think for v3 of older WooCommerce. After that, I did not get much time to work on WooCommerce products. But since then I was getting some queries on how to achieve the same for the latest version of Woo. So thought of doing some research. However when I saw their documentation today, it still says version 3, but everything is changed. Ironic.
Anyways, I quickly set up a local dev environment and tried to create a custom endpoint today. It’s much easier now. Since WordPress integrated REST API in it’s core, we can just register a route which would simply have wc/v3
as the $namespace
, custom
as the $route
, and callback in the $args. This will have a custom WC Endpoint!
Code is as follows:
function get_custom( $data ) {
return array( 'custom' => 'Data' );
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wc/v3', 'custom', array(
'methods' => 'GET',
'callback' => 'get_custom',
));
})
You can add the above code in your theme or custom plugin.
However, if you want to extend WooCommerce REST Controllers, you can use woocommerce_rest_api_get_rest_namespaces
filter.
1. Create a Custom REST Controller Class.
class WC_REST_Custom_Controller {
/**
* You can extend this class with
* WP_REST_Controller / WC_REST_Controller / WC_REST_Products_V2_Controller / WC_REST_CRUD_Controller etc.
* Found in packages/woocommerce-rest-api/src/Controllers/
*/
protected $namespace = 'wc/v3';
protected $rest_base = 'custom';
public function get_custom( $data ) {
return array( 'custom' => 'Data' );
}
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
'methods' => 'GET',
'callback' => array( $this, 'get_custom' ),
)
);
}
}
2. Then register this custom controller in the wc/v3
namespace with the help of hook as follows:
add_filter( 'woocommerce_rest_api_get_rest_namespaces', 'woo_custom_api' );
function woo_custom_api( $controllers ) {
$controllers['wc/v3']['custom'] = 'WC_REST_Custom_Controller';
return $controllers;
}
Now navigate to your-site/wp-json/wc/v3/custom
and test your custom endpoint. It should return {"custom":"Data"}
or whatever you return in WC_REST_Custom_Controller
‘s get_custom
method.
14 comments
Brilliant! I’ve been combing documentation to be able to build a custom callback. Have written dozens of lines of code to test various scenarios. This example makes everything clear and suddenly it seems so simple!
Hi, great tutorial! Thank you! Is it also possible to create endpoint (POST) to update items? Batch update product variations is possible only for one single product in official Woo API. So product variations can be updated (e.g pricing) only by parent Product, one by one. I am thinking about to create a custom API endpoint.
Yes, creating post request endpoint is very similar to get request. It goes something like this:
register_rest_route( 'namespace', 'route', array( 'methods' => 'POST', ... ) );
You can refer the register_rest_route documentation for in depth detailsThanks for this, how do you then validate woocommerce api keys?
Amazing just the right answer, it’s probably a solution that should be taken with caution due to security issues and wordpress really offers many other approaches to common problems but still a great solution
Many thanks! Keep it up.
New to PHP here & worked like a charm! I just wanna ask.. if i get the url from the request, do I need to do some sanitization on the url? if i get it like this? $slug = $data->get_param( ‘slug’ );
If I understand it correctly, you would like to return a url in the API response. In that case, it is rather good to use escape functions as sanitization is more suited to clean input data.
I don’t understand in which files it needs to be registered? Sorry, I’m just beginner to php
You can add the code in your theme. For example,
wp-content/themes/yourtheme/functions.php
I have been trying to remove a product in WP Woocommerce cart through REST API call from another application but I couldn’t found the endpoint in latest v3 version. can you post how to do it and how to add WC store cart Nonce to API call
It should be easy to achieve using
WC()->cart->remove_cart_item( $cart_item_key );
in your custom endpoint.$cart_item_key
depends on which exact product you want to remove from the cart.Please, can you explain where to put this files or what exact files we should add this code.
You can add the code in your theme or custom plugin as mentioned in the post. In theme, you can add under functions.php. If custom plugin, add the code in main plugin php file.