Skip to content

Instantly share code, notes, and snippets.

@debabratakarfa
Last active April 28, 2019 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debabratakarfa/c956191803f2d0c6de9e8090a471e8bc to your computer and use it in GitHub Desktop.
Save debabratakarfa/c956191803f2d0c6de9e8090a471e8bc to your computer and use it in GitHub Desktop.
WooCommerce Custom Rest API Endpoint v3 [creating for https://domain.tld/wc-api/v3/my-custom-endpoint]
add_action( 'woocommerce_api_loaded', 'my_plugin_load_api' );
add_filter( 'woocommerce_api_classes', 'my_plugin_add_api' );
function my_plugin_load_api() {
include_once 'your-api-endpoint-class.php';
}
function my_plugin_add_api( $apis ) {
$apis[] = 'YOUR_API_ENDPOINT_CLASS_NAME';
return $apis;
}
class YOUR_API_ENDPOINT_CLASS_NAME extends WC_API_Resource {
protected $base = '/my-custom-endpoint';
public function register_routes( $routes ) {
$routes[ $this->base ] = array(
array( array( $this, 'get_custom' ), WC_API_Server::READABLE )
);
return $routes;
}
public function get_custom() {
return array( 'custom' => 'Data' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment