Update ACF Field After Easy Digital Downloads (EDD) Purchase

Creating custom functionality that helps me work efficiently is vital to improving my overall productivity. Many of the functionality snippets I create are tedious to setup, but address long term productivity and automation. The tutorial below is an example of such functionality.

The Goal – Automatically update the value of an ACF (Advanced Custom Fields) user profile meta field after an Easy Digital Download (EDD) purchase.

This tutorial assumes you have created an ACF field for the user’s profile. Below is a screenshot of the Annual Support field I have created to show on my user edit screen.

ACF Annual Support Meta

Updating the ACF Field after an EDD Purchase

Here is the function used to update the ACF field after a purchase via EDD (Easy Digital Downloads). This is placed in your functions.php file.

<?php
function wd_support_plan_meta_update( $payment_id ) {

	$payment_meta = edd_get_payment_meta( $payment_id );
	$cart_details = $payment_meta['cart_details'];
	$currentUser = get_current_user_id();
	$acf_id = 'user_' . $currentUser;

	foreach( $cart_details as $item ) {
		if( $item['id'] == ITEM_ID ) {
			update_field( 'annual_support', 'yes', $acf_id );
		}
	}

}
add_action( 'edd_complete_purchase', 'wd_support_plan_meta_update' );

This function fires on the edd_complete_purchase action which, as you might guess, happens when a purchase is complete. The variables used are:

  • $payment_meta – The payment information associated with the purchase
  • $cart_details – Allows us to loop over each item in the cart using the associated purchase
  • $currentUser – Used to get the purchaser’s user ID
  • $acf_id – reformats the user ID variable in to the format needed to update the customer meta properly for ACF

Here is how the function works:

  1. Loop over each item in the $cart_details array
  2. Check if the item has the specified ITEM_ID (note that you will change this to the ID of the EDD Download that’d you like to associate this function with)
  3. If the ID matches, update the ACF field for the current user*

* You’ll want to change annual_support to the meta key for your field and the yes value to whatever you’d like the meta value to be upon update.

That’s all there is to it! Now when a user purchases the specified download the meta key for the associated ACF field will automatically update.

If you haven’t used ACF before I strongly recommend it. Check out my simple review of ACF here.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *