Last active
April 23, 2021 14:38
-
-
Save dparker1005/801918788867d7f68fc0a6c5e09a2ab8 to your computer and use it in GitHub Desktop.
Send users' next payment date to Mailchimp.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Copy from below here... | |
/** | |
* Send users' next payment date to Mailchimp. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpromc_update_on_subscription_payment_completed( $morder ) { | |
if ( ! function_exists( 'pmpromc_getAPI' ) ) { | |
// PMPro Mailchimp is not active. | |
return; | |
} | |
$user = get_userdata( $morder->user_id ); | |
// Get API and bail if we can't set it. | |
$api = pmpromc_getAPI(); | |
if ( empty( $api ) || empty( $user ) ) { | |
return; | |
} | |
// Get all audiences. | |
$audiences = $api->get_all_lists(); | |
if ( ! empty( $audiences ) ) { | |
foreach ( $audiences as $audience ) { | |
// Check for member. | |
$member = $api->get_listinfo_for_member( $audience->id, $user ); | |
if ( isset( $member->status ) ) { | |
// Update the user's merge fields. | |
pmpromc_add_audience_member_update( $user->ID, $audience->id, $member->status ); | |
} | |
} | |
} | |
} | |
add_action( 'pmpro_subscription_payment_completed', 'my_pmpromc_update_on_subscription_payment_completed' ); | |
function my_pmpro_mailchimp_listsubscribe_fields_nextpayment( $fields, $user ) { | |
// Get user's next payment date. | |
$next_payment_date = pmpro_next_payment( $user->ID ); | |
if ( ! empty( $next_payment_date ) ) { | |
$fields['NEXTPAYMNT'] = date( 'Y-m-d', $next_payment_date ); | |
} else { | |
$fields['NEXTPAYMNT'] = 'None'; | |
} | |
return $fields; | |
} | |
add_action( 'pmpro_mailchimp_listsubscribe_fields', 'my_pmpro_mailchimp_listsubscribe_fields_nextpayment', 10, 2 ); | |
/* | |
* Creates an NEXTPAYMNT merge field in MailChimp. | |
*/ | |
function my_pmpro_mailchimp_merge_field_nextpayment( $merge_fields ) { | |
$merge_fields[] = array( | |
'name' => 'NEXTPAYMNT', | |
'type' => 'text' | |
); | |
return $merge_fields; | |
} | |
add_filter( 'pmpro_mailchimp_merge_fields', 'my_pmpro_mailchimp_merge_field_nextpayment' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment