Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Last active June 24, 2020 15:52
Show Gist options
  • Save ronalfy/06e988773f2f62f4fc0b2eccd1b2e7e4 to your computer and use it in GitHub Desktop.
Save ronalfy/06e988773f2f62f4fc0b2eccd1b2e7e4 to your computer and use it in GitHub Desktop.
Paid Memberships Pro - Add Discount Code to MailChimp Export
<?php
/**
* This recipe merges any discount codes that a user has used to the MailChimp export.
*
* 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_pmpro_mailchimp_listsubscribe_fields( $fields, $user ) {
global $wpdb;
$new_fields = array(
'DISCOUNT_CODE_ID' => '',
'DISCOUNT_CODE' => '',
);
$code_id_sql = "SELECT code_id FROM {$wpdb->pmpro_discount_codes_uses} WHERE user_id = %d LIMIT 1";
$new_fields = array();
$code_id = $wpdb->get_var( $wpdb->prepare( $code_id_sql, $user->ID ) );
if ( $code_id ) {
$discount_code_sql = "SELECT code FROM {$wpdb->pmpro_discount_codes} WHERE id = %d LIMIT 1";
$discount_code = $wpdb->get_var( $wpdb->prepare( $discount_code_sql, $code_id ) );
if ( $discount_code ) {
$new_fields = array(
'DISCOUNT_CODE_ID' => absint( $code_id ),
'DISCOUNT_CODE' => sanitize_text_field( $discount_code ),
);
}
}
if ( ! empty( $new_fields ) ) {
$fields = array_merge( $fields, $new_fields );
}
return $fields;
}
add_filter( 'pmpro_mailchimp_listsubscribe_fields', 'my_pmpro_mailchimp_listsubscribe_fields', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment