Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Last active August 13, 2024 15:13
Show Gist options
  • Save kimwhite/19b375eb5747e8461a728127c2a1b19c to your computer and use it in GitHub Desktop.
Save kimwhite/19b375eb5747e8461a728127c2a1b19c to your computer and use it in GitHub Desktop.
This function adds a default commissionrate when creating affiliate code at checkout.
<?php // do not copy this line.
/**
* This recipe will allow you to set a default commission when using "Automatically create affiliate code?".
* This setting can be found in your level settings.
*
* For use with Affiliate Add On https://www.paidmembershipspro.com/add-ons/pmpro-lightweight-affiliate-tracking/
* 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/
*/
// change $commissionrate on line 33 as needed.
// Remove the existing function if it exists
remove_action( 'pmpro_after_checkout', 'pmpro_affiliates_generate_affiliate_after_checkout', 10, 2 );
// Add your custom function
function custom_pmpro_affiliates_generate_affiliate_after_checkout( $user_id, $morder ) {
global $wpdb;
// Get some info
$user = get_userdata($user_id);
$pmpro_level = pmpro_getSpecificMembershipLevelForUser( $user_id, $morder->membership_id );
// Generate the affiliate after membership checkout
$pmpro_create_affiliate_level = get_option( 'pmpro_create_affiliate_level_' . $pmpro_level->id );
$code = pmpro_affiliates_getNewCode();
if ( ! empty( $pmpro_create_affiliate_level ) ) {
$days = intval( apply_filters( 'pmproaf_default_cookie_duration', 30, $user_id, $pmpro_level ) );
// Set the default commission rate to 10%
$commissionrate = 0.10;
$sqlQuery = "INSERT INTO $wpdb->pmpro_affiliates (code, name, affiliateuser, trackingcode, cookiedays, enabled, commissionrate) VALUES('" . esc_sql( $code ) . "', '" . esc_sql( $user->display_name ) . "', '" . esc_sql( $user->user_login ) . "', '', $days, '1', $commissionrate)";
$wpdb->query( $sqlQuery );
}
}
add_action( 'pmpro_after_checkout', 'custom_pmpro_affiliates_generate_affiliate_after_checkout', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment