A
A
aliasst2019-11-14 09:23:35
WordPress
aliasst, 2019-11-14 09:23:35

How to handle API requests going to our store website?

The site should receive Post requests for such URLs, domen.com/order/12345/reserve , domen.com/order/12345/status, in response, the store should receive a response about the status of the order. The numbers in the url are the id of the order. That is, each order has its own url. How to process them? It is not necessary to create a directory for each order as in url, and process the request there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2019-11-14
@aliasst

aliasst , WP has a built-in REST API and it is possible to add your own endpoints and write your own handlers for them. You can see the available routes at domain.com/wp-json/ all routes for woocommerce are in '/wc/v2/' there are routes for getting information about orders. But they all require authorization. You can add your route to functions.php like this:

register_rest_route( 'my_namespace/v1', '/orders/(?P<order_id>[\\d]+)/status', array(
        'methods' => 'GET',
        'callback' => function ($data) {
            $order = wc_get_order($data['order_id']);
            if (!$order) {
                return false;
            }

            return ['order_status' => $order->get_status()];
        },
        'args' => array(
            'order_id' => array(
                'default' => null,
                'required' => true
            ),
        )
    ) );

The result will be like this:
5dccfead8ef6d487750742.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question