# 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>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yaycommerce.com/yaymail/addons/conditional-logic/logic-for-customer-role.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
