> For the complete documentation index, see [llms.txt](https://docs.yaycommerce.com/yaymail/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yaycommerce.com/yaymail/addons/conditional-logic/logic-for-customer-role.md).

# Logic for Customer Role

In this tutorial, we’ll make a conditional logic that:

* Shows up in YayMail as **“Customer Role”**.
* Lists **all WordPress user roles dynamically** (Subscriber, Customer, Shop Manager, your custom roles like B2B, etc.).
* Lets you pick one in YayMail’s conditional logic dropdown.
* Displays the element **only if the customer’s role matches the selected role**.

### Step 1: Install a Snippets Plugin

<figure><img src="/files/MKkdwFrJ5BnqMTTigvcY" alt=""><figcaption></figcaption></figure>

### Step 2: Add the Custom Snippet and Run Everywhere

<figure><img src="/files/h9gFVymeCo6VTBEfoP8I" alt=""><figcaption></figcaption></figure>

```
namespace YayMailAddonConditionalLogic\ConditionalLogics;

use YayMailAddonConditionalLogic\Abstracts\BaseConditionalLogic;

/**
 * Conditional Logic: Customer Role
 */
class CustomerRoleLogic extends BaseConditionalLogic {
    private static $instance;

    public static function instance() {
        if ( null === static::$instance ) {
            static::$instance = new static();
        }
        return static::$instance;
    }

    protected function __construct() {
        parent::__construct(
            'customer_role', // Unique key
            __( 'Customer Role', 'yaymail' ), // Shown in YayMail dropdown
            'single_select' // Dropdown (choose one role)
        );
    }

    public function get_options() {
        global $wp_roles;

        $options = [];
        if ( ! empty( $wp_roles->roles ) ) {
            foreach ( $wp_roles->roles as $role_key => $role_data ) {
                $options[] = [
                    'label' => translate_user_role( $role_data['name'] ),
                    'value' => $role_key,
                ];
            }
        }

        return $options;
    }

    public function check_logic( $args ) {
        $condition       = $args['condition'];
        $order           = $args['order']; // WC_Order object
        $condition_value = $condition['value'];

        // Get user ID from order
        $user_id = $order->get_user_id();
        if ( ! $user_id ) {
            return false; // Guest order, no role
        }

        // Get WP_User object
        $user = get_userdata( $user_id );
        if ( ! $user ) {
            return false;
        }

        // Check if customer’s role matches the chosen condition
        return in_array( $condition_value, (array) $user->roles, true );
    }
}

// Register with YayMail
add_action( 'yaymail_register_conditional_logics', function( $conditional_logics ) {
    $conditional_logics->register( CustomerRoleLogic::instance()->get_logic_data() );
});

```

### Step 3: Check the New Conditional Logic in YayMail

<figure><img src="/files/f9RUfpUjipruEw4bHYpv" alt=""><figcaption></figcaption></figure>

For more details, you can check our tutorial video below.

{% embed url="<https://youtu.be/lEEOC-au7H0>" %}
