Registering after payment actions

In Easy Digital Downloads 2.8, the 'after payment actions' were introduced, which allow developers a way to add process intensive actions when a payment completes without affecting the speed and performance of the end user completing the purchase. This is a guide on how to add an after payment action.

Introduction

From its inception, Easy Digital Downloads has been built around the concept of extensibility. It’s what has lead our platform to have over 250 official add-ons and themes. Over time, as more and more extensions began performing actions after a payment is completed, the checkout process may not seem to complete as quickly. Most of these actions are not ones that are ‘required’ for the purchase to be completed. Things like administrative emails, push notifications, commissions emails, and many other integrations can be delayed slightly as to not affect the purchase completion process. In version 2.8 we’re introducing ‘After Payment’ actions. This new ‘non-transactional’ hook acts exactly like the edd_complete_purchase action that has been previously used for these types of integrations, including the same parameters. So for developers wanting to integrate with the new processing method, it could be as simple as changing the hook you are using.

The hook ( edd_after_payment_actions) triggers via WP_Cron just a short time after the payment is marked as completed. Because it fires with WP_Cron, this means that process intensive activities do not affect a user’s load time of your site. Here is an example of how you can use this in your integrations:

<?php
function eddwp_trigger_after_payment_actions_example( $payment_id = 0, $payment = false, $customer = false ) {
	/**
	 * The $payment variable contains the EDD_Payment object that has been completed.
	 * The $customer variable contains the EDD_Customer object that the payment belongs to.
	*/
}
add_action( 'edd_after_payment_actions', 'eddwp_trigger_after_payment_actions_example', 10, 3 );

There is a filter if you choose not to use after action hooks, in which case, EDD will fallback and perform the actions at the time of completion, and avoid the delayed process.

Key points about after payment actions

  • Triggers via WP_Cron
  • Able to be disabled via filter
  • By default, it is scheduled for 30 seconds after the purchase, but can be changed via a filter.
  • A post_meta key of _edd_complete_actions_run is stored for the payment to report when the action run (in GMT)
  • A payment note is recorded stating the actions were run.

NOTICE: Because this ‘after payment actions’ system is dependent on WP_Cron, it should not be used for things considered ‘mission critical’. It’s simply a way to offload expensive integrations with APIs and large sets of data that do not directly affect the purchase data.