• Resolved jowo

    (@jowo)


    After updating Woocommerce to latest I am unable to create a custom REST API endpoint.

    This used to work:

    add_action('rest_api_init', 'api_reduce_stock_endpoint');
    function api_reduce_stock_endpoint(){
    register_rest_route('wc/v3/products', 'dec', array(
    'methods' => 'PUT',
    'callback' => 'reduce_stock_by_sku',
    'permission_callback' => function($request){
    return is_user_logged_in();
    }
    ));
    }

    This does no longer work and breaks the /wp-json/wc/v3/products/ endpoint.

    Has there been changes to this?


    The Legacy API is activated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Hi @jowo, I’m unable to reproduce the issue with your snippet. Could you please provide some more information?

    1. What’s the exact WooCommerce version you’re using?
    2. What’s the response that you get when you try to reach the endpoint?
    3. Do you have the Legacy REST API plugin installed?
    Thread Starter jowo

    (@jowo)

    Woocommerce 8.8.2

    The response is a massive JSON response starting with:

    {
    
    ? ? "namespace": "wc/v3/products",
    
    ? ? "routes": {
    
    ? ? ? ? "/wc/v3/products": {
    
    ? ? ? ? ? ? "namespace": "wc/v3",
    
    ? ? ? ? ? ? "methods": [
    
    ? ? ? ? ? ? ? ? "GET",
    
    ? ? ? ? ? ? ? ? "GET",
    
    ? ? ? ? ? ? ? ? "POST"
    
    ? ? ? ? ? ? ],
    
    ? ? ? ? ? ? "endpoints": [
    
    ? ? ? ? ? ? ? ? {
    
    ? ? ? ? ? ? ? ? ? ? "methods": [
    
    ? ? ? ? ? ? ? ? ? ? ? ? "GET"
    
    ? ? ? ? ? ? ? ? ? ? ],
    
    ? ? ? ? ? ? ? ? ? ? "args": {
    
    ? ? ? ? ? ? ? ? ? ? ? ? "namespace": {
    
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? "default": "wc/v3/products",
    
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? "required": false
    
    ? ? ? ? ? ? ? ? ? ? ? ? },
    
    ? ? ? ? ? ? ? ? ? ? ? ? "context": {
    
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? "default": "view",
    
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? "required": false
    
    ? ? ? ? ? ? ? ? ? ? ? ? }
    
    ? ? ? ? ? ? ? ? ? ? }
    
    ? ? ? ? ? ? ? ? },

    Declaring all routes and parameters. Looks like a JSON schema?

    Yes the legacy plugin is installed and activated.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @jowo

    Thank for sharing further details.

    For reference, these particular forums are meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    Also, if you believe it is a bug, you can create a bug report in our GitHub repo here, where our developers, including @konamiman, can provide further insights.

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Hi @jowo, I’m still unable to reproduce the issue. However, may I suggest you to register your endpoint like this instead:

    register_rest_route('wc/v3', 'products/dec', array(...

    If this still doesn’t work, could you please create an issue in the WooCommerce repository in GitHub? As Shameem pointed out that’s the better place to continue the discussion.

    Thread Starter jowo

    (@jowo)

    Thanks, this works.

    Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Great, thanks for letting me know. I’ll mark this thread as resolved then.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom REST API endpoint using rest_api_init no longer works’ is closed to new replies.