Deprecated. Please refer How to create WooCommerce API V3 Custom Endpoint here.
WooCommerce is great. But when it comes to API, it requires a little work to do some modifications. Let us take an example of custom endpoints. To make your own woocommerce api endpoint to return custom content, follow this tutorial.
- Create your own plugin. WordPress Docs will help you achieve this
- In plugins main file add the following code: (Don’t forget opening php tag.)
add_action( 'woocommerce_api_loaded', function(){ include_once( 'class-wc-api-custom.php' ); }); add_filter( 'woocommerce_api_classes', function( $classes ){ $classes[] = 'WC_API_Custom'; return $classes; });
- Create a file
class-wc-api-custom.php
in plugin’s root directory and add the following code in that file:<?php class WC_API_Custom extends WC_API_Resource { protected $base = '/custom'; 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' ); } }
Now your custom endpoint is wc-api/v3/custom
which will return content from get_custom
function.
9 comments
[…] http://kart.tk/woocommerce-api-custom-endpoint/ […]
I tried same but it said ” Class ‘WC_API_Custom’ not found ” any idea ? or any new way to this
WC api used in this post is deprecated. I will post updated version soon!
That’s grate!
Thanks.
Please describe more, especially about this liene: “array( array( $this, ‘get_custom’ ), WC_API_Server::READABLE )”:
What is WC_API_Server::READABLE? and why you use nested arrays?
WC api used in this post is deprecated. I will post updated version soon!
Hello,
Possible to know when the update will be available?
Thanks a lot !
Best
Hi Karthik! Do you know if it is posible with the new version of WC to make a custom endpoint?
[…] To create custom endpoint like wc-api/v3/custom you can look at my endpoint tutorial. […]
Created new post for latest v3 API. You can find it here: /woocommerce-api-custom-endpoint-v3