How to assign a custom user role to an affiliate

Affiliate for WooCommerce plugin helps you to add, manage affiliates, and track performance from a single place – commissions, sales, payouts, leaderboard, etc. Earn money without hard work. Increase your brand outreach, and get potential customers.

Note

This is a Developer level documentation. We are happy to provide guidance, but we are unable to provide support or help to suit your store. Only use it if you are comfortable with PHP and custom coding and troubleshooting on your own. If you are unfamiliar with code/templates, you may need to hire a developer for assistance.

Tips to use the below code on your site

↑ Back to top

You can securely add code snippets to your site using either of the methods mentioned here: How to properly add WooCommerce custom code.

We recommend backing up your site before adding custom code and testing it thoroughly in a staging environment.

Assign a custom user role to the affiliate upon approval

↑ Back to top

When a user signs up for your affiliate program they are assigned a user role set under the WordPress Admin > Settings > General > New User Default Role.

However, there may be instances where you want to assign another user role to users while approving as affiliates, whether the approval is automatic or manual (learn more).

The following code snippet will help to add a user role to a user while approving as an affiliate:

// Function to be executed during the 'init' action.
add_action( 'init', 'sa_afwc_init_action' );
function sa_afwc_init_action() {
    add_action( 'afwc_affiliate_approved', 'sa_afwc_trigger_auto_approval' );
}

function sa_afwc_trigger_auto_approval( $user_id = 0 ) {
	if( empty( $user_id ) ) {
		return;
	}

	add_action( 'profile_update', 'sa_afwc_add_user_role_upon_approval' );
	sa_afwc_add_user_role_upon_approval( $user_id );
}

function sa_afwc_add_user_role_upon_approval( $user_id = 0 ) {
        if( empty( $user_id ) ) {
		return;
	}

	$user = get_user_by( 'ID', $user_id );
	$user->add_role( 'affiliate' ); // the user role that you want to add upon approval.
}

Remember to replace the user role with the role you wish to assign to your affiliates under the function sa_afwc_add_user_role_upon_approval as mentioned in the comment.

Feature request

↑ Back to top

Have a feature request or enhancement suggestion for Affiliate For WooCommerce? Submit a request or send it to us from here.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.