Nikolas Theologos
Nikolas Theologos

Reputation: 63

WooCommerce API v3 custom endpoint

I would like to make a custom endpoint in WooCoommerce API v3 in order to fetch some customers from eshop. I know that there is an endpoint available in API v3 but it does not fill the project's specifications.

I've checked this: https://docs.woothemes.com/document/hooks/ but no luck. When I use this action, the response format is in HTML and in JSON.

Can anyone help me with this?

Upvotes: 6

Views: 12610

Answers (2)

vanduc1102
vanduc1102

Reputation: 6255

You can add a new endpoint base on Wordpress API Init

to your main file in plugin or functions file in theme



function get_custom( $request ) {
    return array( 'custom' => 'Data' , "request"=> $request->get_params() );
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'wc/v3', 'custom', array(
        'methods' => 'GET', // array( 'GET', 'POST', 'PUT', )
        'callback' => 'get_custom',
    ));
});

now simply call it with curl

curl http://localhost:8080/wp-json/wc/v3/custom\?message=HelloWorld

Upvotes: 3

Karthik
Karthik

Reputation: 1751

To create custom endpoint like wc-api/v3/custom you can look at my endpoint tutorial.

The main step here is to create a custom class and return that to woocommerce_api_classes filter like so:

add_filter( 'woocommerce_api_classes', function( $classes ){
        $classes[] = 'WC_API_Custom';
        return $classes;
    }
);

After that you can use WC_API_Custom to return custom content.

Upvotes: 17

Related Questions