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

Step 2: Add the Custom Snippet and Run Everywhere

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

Last updated
Was this helpful?