This isn’t rocket science, but the WooCoommerce documentation (as robust and complete as it is) isn’t clear about how to hook into a PayPal IPN successful charge webhook call back thingy—yes, that’s its technical name. In fact “isn’t clear” insinuates some level of recognition from them that something like this can be done. But no, not one word about it. Geez. Anyways, like usual, I digress.
But, if you’re like me, you ASSUME everything can be done. I know this can be done. I just have to dig. Where is my shovel? But I’m also curious. Is this a hush hush thing? Like maybe, if they don’t ask, we won’t have to tell that it’s as easy as writing a function. Thank the dogs for StackOverflow—check out this post.
From information garnered from that post and some elbow grease, here is the function I wrote that allows you to run your own damn code after PayPal calls WooCommerce back, which is apparently super top secret. It’s also posted here.
add_action( 'valid-paypal-standard-ipn-request', 'handle_paypal_ipn_response', 10, 1 ); function handle_paypal_ipn_response( $formdata ) { if ( !empty( $formdata['invoice'] ) && !empty( $formdata['custom'] ) ) { if( $formdata['payment_status'] == 'Completed' ) { // unserialize data $order_data = unserialize( str_replace('\"', '"', $formdata['custom'] ) ); // get order $order_id = $order_data[0]; $order = new WC_Order( $order_id ); // got something to work with? if ( $order ) { // get user id $user_id = get_post_meta( $order_id, '_customer_user', true ); // get user data $user = get_userdata( $user_id ); // get order items $items = $order->get_items(); // loop thru each item foreach( $items as $order_item_id => $item ) { $product = new WC_Product( $item['product_id'] ); // do extra work... } } } } }
Leave a Reply