50

After a long search, I found this post:

WooCommerce hook for "after payment complete" actions

which talks about creating web hooks in WooCommerce to notify a script to do...something...doesn't matter too much what.

I've also read everything I can find in WooCommerce docs.

but I need some kind of documentation or guidance on actually writing the handler on the other end.

My goal is to receive a payment complete notification and then move the user to a different list (a customer list rather than a prospects list) after purchase - I use PHPlist in house as my list manager. Pretty sure I can deal with that part, if I can just get the listener going...

But..I don't know what the web hook sends, how to get it to send data that I want, and what to do with the listener.

I did also find this:

https://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-order-id-for-payment-notification-to-external-webservice?replies=4

which - MIGHT be helpful? i'm still not sure where to begin with the listener, or if this post is valid still, given that it's a couple of years old...

Maxime
  • 8,645
  • 5
  • 50
  • 53
Aaron Trumm
  • 679
  • 1
  • 5
  • 7
  • Seems like there should be an easy way to attach queryArgs to the Woo WebHook so you don't have to go through the trouble of writing PHP code to hook payment complete and send the data yourself. It's a common use case, however from reading the answers so far, that does not appear to be possible to do without writing PHP. – Danger Jan 08 '17 at 16:16
  • It can be useful? https://stackoverflow.com/a/66538110/10447197 – Vincenzo Di Gaetano Mar 09 '21 at 20:03

3 Answers3

81

The woocommerce_payment_complete hook is fired when the payment is completed. The only variable passed is the order id, though from that you can get the order object, and ultimately the user.

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );
    $user = $order->get_user();
    if( $user ){
        // do something with the user
    }
}
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Thanks a bunch!...I'll have to play with this info and see where I get.. :) – Aaron Trumm Nov 20 '15 at 20:09
  • ok yes now I have a couple questions... the piece: 'if($user){' is that going to return false if the order is a guest order (ie: they didn't create or have an account)? all I really need to grab (for now ;) ) is billing email and I guess the product id(s)... the other question is...i'm not real sure what i'm going to do with // do something with user - somehow send my data in the request - but not sure how that's done (can you tell this is my first webhook? :) ) btw @scriptonomy requestb.in is awesome! thanks! :) – Aaron Trumm Nov 20 '15 at 20:30
  • 1
    I don't think I'd call this a webhook, this is a WordPress action hook. Yes, `$user` will be false if there is no registered user. If you just need the billing email you can use `$order->billing_email`. I didn't know exactly what you were doing, so the above is just an example. `$order->get_items()` will get you an array of the items purchased. – helgatheviking Nov 20 '15 at 21:03
  • ahh thanks - that's helpful...so what I'm doing is.. I've got a webhook setup in woocommerce->settings->api - for woocommerce_payment_complete action - I want it to send my listener the email address of the order and the products - my listener (which I haven't even gotten to yet! :) ) will check that email and product and move the address to a different list in phplist - so people will generally be on my prospects list, and then once they buy something, will be moved to a customer list - so first I'm trying to get the webhook to send the data I want.. – Aaron Trumm Nov 20 '15 at 21:11
  • From what I can tell [here](https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-webhook.php#L266) the order created webhook, would deliver the the order object (maybe in json). Weirdly, I don't see an existing webhook "topic" for order completed. Is your list code on the same domain? You could probably proceed with my code above and not need a webhook. I believe that more for integrating with an different domain. – helgatheviking Nov 20 '15 at 22:34
  • thanks for the discussion. - yes I've actually got it sending the way I want, using your code (with additions of course) and the "webhook" totally turned off :) The webhook itself just sends the order id in json and I don't see how that's helpful but I guess it doesn't matter. I still have to write the handler/listener - but that's just a matter of parsing the json and doing stuff - i'll post the code I'm using for the action hook in an answer for others's reference :) – Aaron Trumm Nov 21 '15 at 17:50
  • 1
    I believe we should be using `$order->get_user()` here, and not `$this`, as get_user is a method on the WC_Order class, and we are not within that class. – plushyObject May 17 '17 at 16:28
17

with the help from @helgatheviking and @Scriptonomy I settled on this code, with NO webhook enabled in woocommerce->settings->api->webhooks:

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){  
  $order = wc_get_order( $order_id );
  $billingEmail = $order->billing_email;
  $products = $order->get_items();

foreach($products as $prod){
  $items[$prod['product_id']] = $prod['name'];
}

$url = 'http://requestb.in/15gbo981';
// post to the request somehow
wp_remote_post( $url, array(
 'method' => 'POST',
 'timeout' => 45,
 'redirection' => 5,
 'httpversion' => '1.0',
 'blocking' => true,
 'headers' => array(),
 'body' => array( 'billingemail' => $billingEmail, 'items' => $items ),
 'cookies' => array()
 )
);

Now I just have to write the listener :) This is the body of the request that gets sent (which I can see at requestb.in):

billingemail=%22aaron-buyer%40thirdoptionmusic.com%22&items%5B78%5D=Cult+Of+Nice&items%5B126%5D=Still&items%5B125%5D=The+Monkey+Set
Aaron Trumm
  • 679
  • 1
  • 5
  • 7
2

If you so desire to inspect the web hook request makeup, I suggest you head over to requestb.in and setup a bin. Thus allowing you to inspect the request and formulate an action handler.

Hint: the webhook request sends relative information in the body of the request as JSON formatted data. Once you decode the body, it's easy to traverse it and extract the needed information.

On a different leg of the answer, I point you to @helgatheviking answer and use the woocommerce_payment_complete hook. Once inside the hook, fire off a curl POST request and insert in the body any request handler dependencies. You will extract those dependencies from the $order_id.

Scriptonomy
  • 3,975
  • 1
  • 15
  • 23
  • Same thing I said to @helgatheviking above - thanks a bunch! Even knowing that it's JSON helps lol. I'll play around - I may be back asking questions... :) – Aaron Trumm Nov 20 '15 at 20:10
  • You could also use [`wp_remote_post`](https://codex.wordpress.org/Function_Reference/wp_remote_post). – helgatheviking Nov 20 '15 at 21:06