What Woocommerce function is called on PayPal IPN response?

I’m having a problem figuring out what function gets called when a payment is completed with Woocommerce and PayPal sends the IPN.

The IPN is being received because PayPal log file is being updated as soon as I click Pay, but I can’t figure out what function is writing to that file.

Read More

I need to figure out if there is already a built in functionality to send emails to the admin when an order is created, and where this happens.

If it does exist I need to modify it to email other people too, and if not then I need to create it myself, but I need to know where to put the code.

Related posts

Leave a Reply

2 comments

  1. Checking the file /wp-content/plugins/woocommerce/classes/gateways/paypal/class-wc-paypal.php, we see that there’s an action hook inside the function check_ipn_response:

    if ($this->check_ipn_request_is_valid()) :
    
        header('HTTP/1.1 200 OK');
    
        do_action("valid-paypal-standard-ipn-request", $_POST);
    

    You can hook into it like this:

    add_action( 'valid-paypal-standard-ipn-request', 'so_12967331_ipn_response', 10, 1 );
    
    function so_12967331_ipn_response( $formdata )
    {
        // do your stuff
    }
    
  2. Building on @brasofilo’s answer, I had to do extra work for each product for the current order.

    Note: I’m new to (un)serializing data, so I don’t know why I had to unescape the double quotes to get unserialize() to work. It threw an error, otherwise. Maybe there’s a better way to handle this.

    function so_12967331_ipn_response( $formdata ) {
    
        if ( !empty( $formdata['invoice'] ) && !empty( $formdata['custom'] ) ) {
    
            if( $formdata['payment_status'] == 'Completed' ) {
    
                if( is_serialized( $posted['custom'] ) ) {
    
                    // backwards compatible
                    // unserialize data
                    $order_data = unserialize( str_replace('"', '"', $posted['custom'] ) );
                    $order_id = $order_data[0];
    
                } else {
    
                    // custom data was changed to JSON at some point
                    $order_data = (array)json_decode( $posted['custom'] );
                    $order_id = $order_data['order_id'];
    
                }
    
                // get order
                $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...
    
                    }   
                }   
            }
        }
    }