How to Add Custom WooCommerce URLs with the woocommerce_api_(action) Hook

WooCommerce’s woocommerce_api_(action) Action hook allows the plug-in to add a custom callback to a URL. When the URL is accessed, the function or method in the custom callback will be executed. This API is inWooCommerce payment gatewayIn addition to payment gateways, when WooCommerce interacts with third-party services and needs to receive data returned by third-party services, we also need to use this API. Detailed information about this API can be found in the WC_API class documentation.

Callback URL form

Before WooCommerce 2.0, we could access the custom callback URL using a URL similar to the following.

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

After WooCommerce 2.0, in addition to the URL with parameters above, we can also access our callbacks in a static way.

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' );

Pay attention to the “callback” character in the hook name “woocommerce_api_callback” above. This is our custom callback. The unique name will be displayed in the URL parameters. This name is also needed when obtaining the custom callback URL.

After executing the operations defined in the callback, WooCommerce will exit the operation. Of course, if necessary, we can also jump to other URLs before exiting the operation.

Get custom callback URL

We can obtain the custom callback URL above through a method of a WC() instance.

WC()->api_request_url( 'wc_ezship_send_order' )

We can developWooCommerce payment gatewayOr when using other services, use the above method to obtain the address of the custom callback URL.

Related Posts

Leave a Reply

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