HTTP Post Code Samples
While the standard configuration covers most integration scenarios, some APIs require custom headers, non‑standard data packaging, or the ability to process responses from the receiving server. This guide covers the three developer hooks that give you complete control over HTTP POST behaviour.
Overview of HTTP POST Hooks
The plugin provides three hooks for customising HTTP POST requests:
|
Hook |
Purpose |
Use Case |
|---|---|---|
|
|
Modify the arguments passed to |
Setting custom headers, changing Content‑Type, or reformatting the request body |
|
|
Process the response received from the remote server |
Parsing API replies and storing returned data (e.g., tracking numbers) as order meta |
|
|
Replace the default POST behaviour entirely |
Switching to GET requests or implementing completely different delivery logic |
These hooks are available only in the Pro version of the plugin and work with both manual and scheduled exports.
Example 1: Set a Custom Content‑Type Header
Some APIs require a specific Content‑Type header. By default, the plugin sends the appropriate header based on your selected output format (e.g., application/json for JSON exports). This example forces the Content‑Type to text/csv:
|
1 2 3 4 5 |
// modify Content-Type for HTTP Post ( Pro version) add_filter('wc_order_export_http_args', function ($args) { $args['headers']['Content-Type'] = "text/csv"; return $args; }); |
This is particularly useful when your receiving endpoint expects a different format than the one the plugin assumes.
Example 2: Send XML as a POST Variable
Some legacy APIs expect XML data to be wrapped inside a named POST variable rather than sent as the raw request body. This example takes the export data (which contains XML) and packages it as xml parameter using http_build_query():
|
1 2 3 4 5 |
// send xml as POST variable "xml" ( Pro version) add_filter('wc_order_export_http_args', function ($args) { $args['body'] = http_build_query( array("xml"=>$args['body']) ); return $args; }); |
After applying this filter, your receiving endpoint can access the XML data via $_POST['xml'].
Important Notes for wc_order_export_http_args
Example 3: Parse a CSV Reply and Save as Order Meta
When your receiving endpoint returns data in a simple format — for example, a plain text reply containing a tracking number and amount separated by a comma — this example parses that reply and stores the values as custom order meta fields.
|
1 2 3 4 5 6 7 8 9 |
// parse and save HTTP reply ( Pro version) add_filter('woe_export_http_response', function ($response) { $parts = explode(",", $response['body'] ); // we get reply as plan text - "Tracking Number, Amount" $order = wc_get_order(WC_Order_Export_Engine::$order_id); $order->update_meta_data( "tracking_number", $parts[0]); $order->update_meta_data( "tracking_price", $parts[1]); $order->save(); return $response; }); |
Accessing the current order ID: The static WC_Order_Export_Engine::$order_id property gives you the ID of the order currently being exported. This allows you to associate response data with the correct order.
Important Notes for woe_export_http_response
Example 4: Send a GET Request with Order Data as Query Parameters
Status change jobs that use the HTTP POST destination may not work as expected because the default action is designed for POST requests. This example demonstrates how to override the default behaviour and send a GET request instead, with order data appended as URL parameters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Status Change job sends GET request , default action won't work add_filter("woe_export_http_custom_action", function($response, $url, $args) { $order = new WC_Order(WC_Order_Export_Engine::$order_id); //make vars here $vars = array(); $vars['key'] = "secretkey"; $vars['id'] = $order->get_id(); $vars['recipient'] = $order->get_shipping_first_name() . " " . $order->get_shipping_last_name(); $vars['company'] = $order->get_shipping_company(); $vars['street'] = $order->get_shipping_address_1(); $vars['city'] = $order->get_shipping_city(); $vars['state'] = $order->get_shipping_state(); $vars['countrycode'] = $order->get_shipping_country(); $vars['zipcode'] = $order->get_shipping_postcode(); $vars['phone'] = $order->get_billing_phone(); //send $url .= "?" . http_build_query($vars); $response = wp_remote_get($url, $args); return $response; },10,3); |
In this example, the receiving endpoint receives all order data as URL query parameters, which is useful for simple webhooks or legacy systems that cannot parse POST bodies.