Webhook for woocomm

Hi,

Im trying to get my form to send a webhook to woocommerce to create an order.

POST to /wp-json/wc/v3/orders

Right now im testing the webhook to webhook.site.

My question is, how do create an array like this:

'line_items' => [
        [
            'product_id' => 93,
            'quantity' => 2
        ],

I can get it to do
‘product_id’ => 93,
‘quantity’ => 2

Thanks!

Hi Paul. It’s not currently possible to define that nested JSON structure via the feed settings. We do have it on the feature request list for consideration by the development team when planning future versions of the add-on and I added a vote for you there.

You could use the gform_webhooks_request_data filter in the theme functions.php file or a custom functionality plugin to create your own array of data, including using nested arrays. The array you return via the filter will then be json encoded by the add-on before being sent to the designated endpoint.

An alternative solution would be to use the third-party Gravity Flow add-on which has an outgoing webhook step which supports defining the request body using json in a textarea setting.

1 Like

Thanks,

I went through the document.

i wrote this snippet.
It works when i sent it to webhook.site but i can’t get it to post an order in woocommerce

The request URL is: {rest_api_url}wc/v3/orders

add_filter( 'gform_webhooks_request_data', 'modify_data', 10, 4 );
function modify_data( $request_data, $feed, $entry, $form ){
$request_data = array (
'line_items' => array (
        'product_id' => rgar( $entry, '15'),
        'meta_data' => rgar( $entry, '10'),
        ),
	
'shipping_lines' => array (
        'total' => rgar( $entry, '1.2'),
        )
);
return $request_data;
}

Is there any logging you can enable on the site where WooCommerce is running? That is where you will need to see what is happening if it worked at the webhook.site.

I’m not sure on other ways to log, but if i go woocommerce-status-logs nothing shows up in logs
(logs enabled)

I tried this snippet, which is almost the same from WooCommerce REST API Documentation - WP REST API v3
and i can’t get it to do anything.

Sorry it’s my first time doing webhooks but im not sure if i’ve done it right or i’ve missed something.

add_filter( 'gform_webhooks_request_data_5', 'modify_data', 10, 4 );
function modify_data( $request_data, $feed, $entry, $form ){
$request_data = array (
	    'payment_method' => 'bacs',
    'payment_method_title' => 'Direct Bank Transfer',
    'set_paid' => true,
	    'billing' => array (
        'first_name' => 'John',
        'last_name' => 'Doe',
        'address_1' => '969 Market',
        'address_2' => '',
        'city' => 'San Francisco',
        'state' => 'CA',
        'postcode' => '94103',
        'country' => 'US',
        'email' => 'john.doe@example.com',
        'phone' => '(555) 555-5555'
			),
	    'shipping' => array (
        'first_name' => 'John',
        'last_name' => 'Doe',
        'address_1' => '969 Market',
        'address_2' => '',
        'city' => 'San Francisco',
        'state' => 'CA',
        'postcode' => '94103',
        'country' => 'US'
    ),
'line_items' => array (
        'product_id' => rgar( $entry, '15'),
        'meta_data' => rgar( $entry, '10'),
        ),
	
'shipping_lines' => array (
        'total' => rgar( $entry, '1.2'),
        )
);
return $request_data;
}

Does the WC Rest API expect your data to be sent as a JSON array which the gform webhook is/not encoding in expected format?

I find the combination of tools like Postman and RequestBin to be invaluable when debugging API request/responses. Sending my webhook to RequestBin shows me the output format exactly while PostMan can help me build up a request structure that API will definitely receive independent of any WP/GForms (or my own) code.

If you do look at Gravity Flow’s outgoing webhook, from your description the RAW body format would be a good approach to build the structure without PHP code (i.e. define the json array in the step settings and use merge tags to populate values.