Skip to content

Instantly share code, notes, and snippets.

@dryan1144
Last active November 27, 2020 23:27
Show Gist options
  • Save dryan1144/3013081cbbb4e47ccdb833dba0c94761 to your computer and use it in GitHub Desktop.
Save dryan1144/3013081cbbb4e47ccdb833dba0c94761 to your computer and use it in GitHub Desktop.
<?php
/***
Update Convertkit email with a Convertkit tag based on EDD download ID
Conditionally set Convertkit tag ID based on EDD download ID
***/
add_action( 'edd_complete_purchase', 'drw_update_ck_after_edd_purchase' );
function drw_update_ck_after_edd_purchase( $payment_id ) {
$edd_payment = new EDD_Payment($payment_id);
$email = $edd_payment->email;
$cart_items = edd_get_payment_meta_cart_details( $payment_id );
foreach ( $cart_items as $download ) {
$download_id = $download['id'];
if ( 321 == $download_id ) {
$ck_tag_id = 4879234;
drw_update_ck_subscriber( $email, $ck_tag_id, 'subscribe' );
} elseif( 430 == $download_id ) {
$ck_tag_id = 4879234;
drw_update_ck_subscriber( $email, $ck_tag_id, 'subscribe' );
}
}
}
<?php
/***
Define Convertkit API key
***/
define('CK_API_KEY', 'your_convertkit_api_key_here');
/***
Send subscriber data to Convertkit
$email str customer email address to send to CK
$endpoint str 'subscribe', 'unsubscribe'
***/
function drw_update_ck_subscriber( $email, $ck_tag_id, $endpoint = null ) {
if ( null == $endpoint ) $endpoint = 'subscribe';
//Add extra fields here if necessary
$args = array(
'email' => $email,
);
$request = wp_remote_post(
'https://api.convertkit.com/v3/tags/' . $ck_tag_id . '/'.$endpoint.'?api_key='.CK_API_KEY,
array(
'body' => $args,
'timeout' => 15,
)
);
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
$return = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment