While I'm trying to extend Woocommerce Rest-API routes with a custom one I face the following problem. I have the following class which tries to hook inside Woocommerce API

public function init()
            add_action( 'woocommerce_api_loaded', array( $this, 'load' ) );
        public function load()
            //This method will be not called
            require_once plugin_dir_path( __FILE__ ).'wc-api-custom.php';
            add_filter( 'woocommerce_api_classes', array( $this, 'register' ) );

but woocommerce_api_loaded will not trigger any callback.

What I do wrong in this case

  • "will not trigger any callback" - you mean your load function is never called? It looks like the hook is only called from class-wc-legacy-api, so it's probably worth adding trace to that to make sure it's being loaded and that your requests are hitting handle_rest_api_requests there. – Rup Jun 24 '19 at 10:38
  • hi thanks for feedback, exactly my load function will not fire, how would I go on with your suggestion? – fefe Jun 24 '19 at 10:42
  • 1
    There are probably better ways to do this, but I normally add error_log calls to the plugin on a test instance and then watch the webserver's error log (or PHP's error log, depending on your setup) to see what's happening. I'd start by putting logs in the class WC_Legacy_API constructor, handle_rest_api_requirements (the 'parse_request' hook), and probably then trace out WC_API_REQUEST_VERSION to see which handle function it's calling. – Rup Jun 24 '19 at 10:49
  • Try dropping in do_action( 'woocommerce_api_loaded' ); to make sure things are triggered. And back to Rup's point, it sounds like you might need to start tracing those calls. Lastly, authors of Woocommerce ask that you not use the same namespace, but rather your own name space. github.com/woocommerce/woocommerce/issues/…. If you use a separate name space, then it's relatively easy to tap into current_user_can() to tie back to Woocommerce functionality to secure your end points. – ChristopherJones Jun 24 '19 at 12:54
  • the point why I want to use the same namespace as woocoomerce is to guard the access to the route with same authentication method used by woocommerce – fefe Jun 24 '19 at 13:01

What if in your register_rest_route() call you can pass it the permission_callback option:

'permission_callback' => function () {
  return current_user_can('customer');
  // OR
  return current_user_can('shop_manager');

That way you are sure that you are dealing with a logged in Woocommerce user??

These are the two roles that Woocommerce is adding to your site. https://docs.woocommerce.com/document/roles-capabilities/

I know it's not answering your question directly, but I hope this helps! If I am way off, I can take the answer down.

| improve this answer | |
  • thanks for feedback, but I think your workaround can not deal with remote requests. I mean I would like to request endpoints from remote server and able to use OAuth to authenticate against the rest-api and woocommerce already has a logic behind. But I appreciate your point with the permission callback if I do not find an appropriate way to integrate my route inside woocommerce this is the way to go – fefe Jun 24 '19 at 14:15
  • I am sure you've seen it, but there are a few options for external requests, here is a link with a few options that WP lays out. developer.wordpress.org/rest-api/using-the-rest-api/… – ChristopherJones Jun 24 '19 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.