Overview

Who is This Guide For?

This guide assumes the following:

  • You are already using the PayPal for WooCommerce plugin to take payments with Express Checkout and/or Payments Pro.
  • You would like to automatically update the WooCommerce order status when payments are completed.

Introduction

The PayPal Standard integration included with WooCommerce uses PayPal IPN to update the order status when payments are completed.  We initially planned to do the same thing with PayPal for WooCommerce, however, we did not like the idea of limiting the power of PayPal IPN.  We decided to build a separate, stand-alone plugin: PayPal IPN for WordPress.

In this guide we will cover the steps involved in building the PayPal IPN for WordPress plugin into your theme in order to automatically update the WooCommerce order status when a PayPal Express Checkout or Payments Pro transaction is completed.

Advantages of Updating with PayPal IPN

  • Correct handling of pending payments (e-checks, payment holds, etc.) so your order status is not updated until a payment actually clears and is fully complete.
  • Updates happen automatically in real-time.  No more manual updates!

Get Our IPN Plugin

Get the plugin version of this solution for quick and easy installation!

Step 1 – Install and Configure PayPal IPN for WordPress

In order to setup automatic updates from your PayPal account to your WordPress / WooCommerce website you will need to install our PayPal IPN for WordPress plugin and configure it with your PayPal account.

Step 2 – Set the PayPal IPN Hook in functions.php

Open the functions.php file in your theme (child) and add the following code.

add_action('paypal_ipn_for_wordpress_payment_status_completed', 'angelleye_update_wc_order_status', 10, 1);

function angelleye_get_paypal_order($raw_custom) {
    if (( $custom = json_decode($raw_custom) ) && is_object($custom)) {
        $order_id = $custom->order_id;
        $order_key = $custom->order_key;
    } elseif (preg_match('/^a:2:{/', $raw_custom) && !preg_match('/[CO]:\+?[0-9]+:"/', $raw_custom) && ( $custom = maybe_unserialize($raw_custom) )) {
        $order_id = $custom[0];
        $order_key = $custom[1];
    } else {
        return false;
    }
    if (!$order = wc_get_order($order_id)) {
        $order_id = wc_get_order_id_by_order_key($order_key);
        $order = wc_get_order($order_id);
    }
    $order_key_value = version_compare(WC_VERSION, '3.0', '<') ? $order->order_key : $order->get_order_key();
    if (!$order || $order_key_value !== $order_key) {
        return false;
    }
    return $order;
}
 
function angelleye_update_wc_order_status($posted) {
    if (!empty($posted['custom']) && ( $order = angelleye_get_paypal_order($posted['custom']) )) {
        $order_id = version_compare(WC_VERSION, '3.0', '<') ? $order->id : $order->get_id();
        if (!empty($order_id)) {
            $order = new WC_Order($order_id);
            $order->update_status('completed');
        }
    }
}

Step 3 – Upload functions.php

All that’s left to do is upload your functions.php file to your web server.

From now on, any orders that come through PayPal and have a matching WooCommerce Order will automatically update the order status to Completed!

Looking for Live Help?

Schedule a live meeting with Drew Angell, PayPal Certified Developer, and get all of your questions or concerns answered.