> 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="https://157604815-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJfBhHG_1ipE5lMX4qT%2Fuploads%2FCjGXhqXV1CC65X5hJt5R%2FPlugins%20required%20for%20custom%20conditional%20logic%20in%20YayMail.png?alt=media&amp;token=2e31a366-3d0a-479f-9cf9-0dfb984657dd" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://157604815-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJfBhHG_1ipE5lMX4qT%2Fuploads%2Fces2Jzwoxv7EhpPsjBgP%2FSnippets%20to%20display%20customer%20roles.png?alt=media&amp;token=5f97c495-730f-49c4-8cd3-19989a46878e" 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="https://157604815-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJfBhHG_1ipE5lMX4qT%2Fuploads%2FuJzOAm6ezc6YFXRVLtp5%2FDisplay%20image%20if%20customer%20role%20matches%20the%20selected%20role.png?alt=media&amp;token=aa334be2-8ba3-4551-8362-bcc1184d56b5" alt=""><figcaption></figcaption></figure>

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

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