In WooCommerce the rest api is used to get data from orders , products , customers..
but how you can customize or add add other api route to get (fields and value) , ok it's exist many techniques
first work directly on the core for example inside :
woocommerce/includes/class-wc-api.php in fuction includes or handle_v1_rest_api_request() or handle_v2_rest_api_request() :
add :
include_once( 'api/class-wc-api-customs.php' );
and you will find apply_filters add you custom class to filters :
$api_classes = apply_filters( 'woocommerce_api_classes', 'WC_API_Customers', 'WC_API_Orders', 'WC_API_Customs', 'WC_API_Products', 'WC_API_Coupons', 'WC_API_Reports', ) );
Now you should add the class class-wc-api-customs.php extends WC_API_Resource the content is :
<?php } class WC_API_ Customs extends WC_API_Resource { /** @var string $base the route base */ protected $base = '/customs'; /** @var string $post_type the custom post type */ protected $post_type = 'shop_cart'; /** * @return array */ public function register_routes( $routes ) { # GET|POST /carts ); return $routes; } $count_customs = 7; return apply_filters('carts_count',$count_customs); } }
Conclusion : we have declared our custom route inside register_routes with name get_count_customs and we created function for done something .
Now you can use by example Api v2 or 3 for call :
wc-api/v3/get_count_customs
2 : plugin :
inside woocommerce-ac.php add :
add_action('woocommerce_api_loaded', 'wpc_register_wp_api_endpoints'); function wpc_register_wp_api_endpoints() { include_once( 'woo-includes/api/v2/class-wc-api-carts.php' ); add_filter('woocommerce_api_classes', 'filter_woocommerce_api_classes', 10, 1); } function filter_woocommerce_api_classes($array) { 'WC_API_Carts', ); }
and inside woo-includes/api/v2 add class-wc-api-customs.php add you code
that is done
Comments