Adding a custom URL to WooCommerce using the woocommerce_api_(action) Action hook

WooCommerce's woocommerce_api_(action) Action hook allows the plugin to add a custom callback to a URL, and when that URL is accessed, the function or method in the custom callback executes, and this API is available in the WooCommerce Payment GatewayIn addition to the payment gateway, when WooCommerce interacts with third-party services and needs to receive the data returned by the third-party services, we also need to use this API, for more information about this API, please refer to the WC_API class documentation.

The form of the callback URL

Prior to WooCommerce 2.0, we could access a custom callback URL with a URL similar to the one below.

http://yoursite.com/?wc-api=CALLBACK

As of WooCommerce 2.0, in addition to the above parameterized URL, we can also access our callbacks statically.

http://yoursite.com/wc-api/CALLBACK/

Add a custom URL

We can add a custom callback URL with code similar to the following:

add_action( 'woocommerce_api_callback', 'callback_handler' );

Note the “callback” character in the hook name “woocommerce_api_callback” above, this is the unique name of our custom callback, which will be shown in the URL parameter. This is the unique name of our custom callback, which will be shown in the URL parameter, and will be used when fetching the URL of the custom callback.

After executing the action defined in the callback, WooCommerce exits the operation, of course, we can jump to another URL before exiting the operation if needed.

Get custom callback URL

We can get the above customized callback URL via a method of a WC() instance.

WC()->api_request_url( 'wc_ezship_send_order' )

We can develop WooCommerce Payment Gatewayor other services, use the above method to get the address of the custom callback URL.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *