Website Building » WooCommerce » How Do I Add Endpoints in WooCommerce?

How Do I Add Endpoints in WooCommerce?

Last updated on January 14, 2023 @ 11:18 am

Adding an endpoint in WooCommerce is easy. There are two ways to do it:

Method 1: Use the WC_REST_Connect_Controller class.

This method is recommended for developers who are familiar with the WooCommerce codebase. To use this method, you need to:

PRO TIP: Endpoints are a URL that is appended to your main WooCommerce shop URL, and they look something like this:

http://yourdomain.com/shop/my-account/payment-methods

They are used to control what content is shown on the My Account page in WooCommerce.

Adding an endpoint is easy, but there are a few things to keep in mind:

1. Make sure you flush the permalinks after adding an endpoint. This can be done by going to Settings > Permalinks and clicking the ‘Save Changes’ button.

2. If you’re using a custom WooCommerce template, you’ll need to edit your template files to account for the new endpoint.

3. Endpoints can conflict with other plugins or themes that use similar URLs. If you run into any issues, try changing the slug for your endpoint (the part after ‘my-account/’).

  1. In your theme’s functions.php file, add the following code:
add_action( 'rest_api_init', 'my_custom_endpoints' );
function my_custom_endpoints() {
   $controller = new WC_REST_Connect_Controller();
   $controller->register_routes();
}
  1. In a new file, create a class that extends WC_REST_Connect_Controller. For example:
class MyCustomEndpoint extends WC_REST_Connect_Controller {
   public function __construct() {
      $this->namespace = 'wc/v1'; // define namespace for your endpoint(s)
   }
   public function register_routes() {
      register_rest_route( $this->namespace, '/my-endpoint', array( // define endpoint URL and method here
         'methods'  => 'GET', // define which methods this endpoint allows: GET, POST, PUT or DELETE
         'callback' => array( $this, 'my_endpoint' ), // callback function to run when this endpoint is called ) );
   public function my_endpoint( WP_REST_Request $request ) { // callback function definition - takes one parameter: WP REST Request object
      // do something here - like return some data back to the caller..
      return true; // don't forget to return something!  Otherwise you'll get a "false" error in the response. 
    } 
} 

Method 2: Use the register REST API route()

This is the recommended approach if you’re not familiar with the WooCommerce codebase. To use this method, you need to:

In your theme’s functions.php file, add the following code:

add action('rest api init','my custom routes');. 
function my custom routes(){. register rest route('wc/v1','my-endpoint',array('methods'=>'get','callback'=>'my custom callback'));    }. function my custom callback(){. // do something here - like return some data back to caller.     return true; // don't forget to return something! Otherwise you'll get a "false" error in response.     }  

That’s it! You’ve now added a new REST API endpoint in WooCommerce.

Morgan Bash

Morgan Bash

Technology enthusiast and Co-Founder of Women Coders SF.