Skip to content

Custom User Role Sync

khungate edited this page Dec 15, 2023 · 2 revisions

Sometimes there is a requirement to cater to diverse user roles beyond the standard 'customer' role. This section discusses the implementation of a filter to support custom user roles in order synchronization, a feature beneficial for administrators who create roles like "club-members".

Overview The ability to synchronize orders based on custom user roles is crucial for businesses that operate with unique user classifications. This feature allows for more flexible and targeted marketing and customer management within the WooCommerce environment.

Key Discussion Points Problem Statement: WooCommerce administrators need to synchronize orders based on custom user roles created via WordPress. Solution: Implementing a filter in WooCommerce to support custom user roles. Existing Filter: mailchimp_campaign_user_roles An existing filter, mailchimp_campaign_user_roles, enables the specification of valid roles for order filtering. This can be leveraged to include custom user roles in the synchronization process.

Example Implementation The filter can be implemented in the functions.php file of your theme. Below is an example snippet:

add_filter('mailchimp_campaign_user_roles', function($allowed_roles) {
    $allowed_roles[] = 'your_custom_role';
    $allowed_roles[] = 'another_custom_role';
    return $allowed_roles;
});

This code allows the inclusion of 'your_custom_role' and 'another_custom_role' in the order synchronization process.

Default Roles By default, 'customer' and 'subscriber' roles are included. The following code illustrates the basic usage of the filter:

$allowed_roles = array('customer', 'subscriber');
$allowed_roles = apply_filters('mailchimp_campaign_user_roles', $allowed_roles);

Custom roles can be added to this array as needed.

Clone this wiki locally