• Resolved marquedigitale

    (@marquedigitale)


    Hi,

    I am trying to create new orders with the Woocommerce API. I send all correct info to Woocommerce, which tries to send me back the whole order data (which I don’t need as I sent it in the first place). The only piece of information I need is the order ID created.

    My issue is, as the created orders are quite big, Woocommerce struggles to send all the data back and I do not receive anything from it.

    So is there a way to tell the API to not send me back all the order data I am already aware of, but instead only the created order ID?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Barry

    (@barryhughes-1)

    Hi @marquedigitale,

    Our REST API is itself built on top of WordPress’s own REST API, and so we can take advantage of some of that functionality. In this case, for instance, we might specify the return fields we are interested in via the _fields parameter. Minimal example:

    POST https://example/wp-json/wc/v3/orders?_fields=id HTTP/1.1
    Content-Type: application/json
    Authorization: Basic abc1234def5678ghi9012jkl
    
    {
      "payment_method": "bacs",
      "payment_method_title": "Direct Bank Transfer",
      "set_paid": true,
      "billing": {
        "email": "john.doe@example.com",
        "phone": "(555) 555-5555"
      }
    }

    This should result in a response looking something like this:

    {
      "id": 12345,
      "_links": {
        "self": [
          {
            "href": "https:\/\/example\/wp-json\/wc\/v3\/orders\/12345"
          }
        ],
        "collection": [
          {
            "href": "https:\/\/example\/wp-json\/wc\/v3\/orders"
          }
        ]
      }
    }

    Though we still have a few bits of information in there that, depending on your perspective, may be superfluous (that is, the _links information), the total amount of information being passed back to the client has been reduced considerably. Does that help?

    …I’ll also take this opportunity to mention we are actively gathering feedback on our REST API:

    github.com/woocommerce/woocommerce/discussions/44199

    Please don’t hesitate to drop any other thoughts or observations in there—be they about the structure of the API, the documentation, or some other aspect—we’re really keen to hear from active users.

    Thread Starter marquedigitale

    (@marquedigitale)

    Hi @barryhughes-1

    Thanks for the quick reply.

    I will try this asap, I think that it will be very helpful because the issue is that my orders have numerous line_items, so I can see the new order in Woocommerce, but I get a server timeout when builing the response. I don’t mind getting a few superfluous data, I just need a tinier response.

    I did not find this in the documentation, is it that I did not search it enough?
    I will try to take time to give you feedback on the REST API, but as far as I am concerned there is nothing wrong with it and it is quite useful and easy to use.

    Thanks again for your help!

    Regards

    Thread Starter marquedigitale

    (@marquedigitale)

    Hi again,

    Quick feedback about this issue: your tip is working like a charm.

    I use a third-party library (Codexshaper/laravel-woocommerce) for the integration of Woocommerce API with Laravel, and it is currently not possible to add query parameters for POST requests with it so I had to make it possible for myself.

    For those who are in the same position as I am, I edited the vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php file, at the end of the createRequest method, right before

    $this->request = new Request(
        $this->buildUrlQuery($url, $parameters),
        $method,
        $parameters,
        $headers,
        $body
    );

    I added

    if($method == 'POST' && $endpoint == 'orders')
        $parameters = array_merge(
            ['_fields' => 'id'], 
            $parameters
        );

    Sure it is not the best way to achieve this, but this is working…

    I’ll go get in touch with Codexshaper to try and get this native with their library.

    Thanks again for the help

    Plugin Author Barry

    (@barryhughes-1)

    Thanks for sharing that!

    I did not find this in the documentation, is it that I did not search it enough?

    It’s actually found in the WordPress REST API docs:

    developer.wordpress.org/rest-api/using-the-rest-api/global-parameters

    I don’t know that we clearly reference those from our own REST API docs, though. We couldn’t definitely make this clearer.

    Thread Starter marquedigitale

    (@marquedigitale)

    Well, I’ll also check these docs in the future 🙂

    Thank you for your help, this thread is resolved.

    Plugin Support Zubair Zahid (woo-hc)

    (@doublezed2)

    Hello marquedigitale,

    We hope you’ve been enjoying your experience with WooCommerce!

    If you feel that our plugin has been beneficial to your business, we would be truly grateful if you could take a few moments to leave a review.

    Your feedback not only supports our continuous improvement but also helps others make informed decisions about using WooCommerce.

    Have a great day!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.