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 using the PayPal IPN for WordPress plugin to automatically update our 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!

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 free 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.

function get_paypal_order($raw_custom) {
        $custom = json_decode($raw_custom);
        if ($custom && is_object($custom)) {
            $order_id = $custom->order_id;
            $order_key = $custom->order_key;
        } else {
            return false;
        }
        $order = wc_get_order($order_id);
        if (!$order) {
            $order_id = wc_get_order_id_by_order_key($order_key);
            $order = wc_get_order($order_id);
        }
        if (!$order || $order->get_order_key() !== $order_key) {
            return false;
        }
        return $order;
    }

    function update_wc_order_status($posted) {
        $order = !empty($posted['custom']) ? get_paypal_order($posted['custom']) : false;
        if ($order) {
            $posted['payment_status'] = strtolower($posted['payment_status']);
            if ('completed' === $posted['payment_status']) {
                $order->add_order_note(__('IPN payment completed', ''));
                $order->payment_complete(!empty($posted['txn_id']) ? wc_clean($posted['txn_id']) : '' );
            }
        }
    }

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

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!

Need Additional Help?

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

AngellEYE PayPal Newsletter Signup

Subscribe To Our Newsletter

Join our list for the latest promotions, news, and updates from AngellEYE and PayPal!

You have Successfully Subscribed!

Pin It on Pinterest