admin管理员组

文章数量:1122832

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

class API_LOADER
{
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

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

class API_LOADER
{
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

Share Improve this question edited Jun 24, 2019 at 10:19 fefe asked Jun 24, 2019 at 9:57 fefefefe 8943 gold badges14 silver badges34 bronze badges 5
  • "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 Commented Jun 24, 2019 at 10:38
  • hi thanks for feedback, exactly my load function will not fire, how would I go on with your suggestion? – fefe Commented Jun 24, 2019 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 Commented Jun 24, 2019 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 Commented Jun 24, 2019 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 Commented Jun 24, 2019 at 13:01
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: hooksExtend Woocommerce rest api routes fails