Skip to content

Instantly share code, notes, and snippets.

@iamsathyaseelan
Created September 24, 2020 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamsathyaseelan/bd653ccf955ca88225a149b466bafdd8 to your computer and use it in GitHub Desktop.
Save iamsathyaseelan/bd653ccf955ca88225a149b466bafdd8 to your computer and use it in GitHub Desktop.
email customizer plus - get customer role shortcode
add_filter('woocommerce_email_customizer_plus_additional_short_codes_list', 'wecpLoadAdditionalShortCodes', 10, 3);
function wecpLoadAdditionalShortCodes($additional_short_codes){
$additional_short_codes['customer.role'] = 'get customer role';
return $additional_short_codes;
}
add_filter('woocommerce_email_customizer_plus_short_code_values', 'wecpLoadAdditionalData', 10, 4);
function wecpLoadAdditionalData($short_codes, $order, $args, $sample){
if (is_array($short_codes)) {
$user_role = '';
if(isset($short_codes['customer']['email']) && !empty($short_codes['customer']['email'])){
$email = $short_codes['customer']['email'];
$user = get_user_by('email', $email);
if(empty($user)){
$user_role = 'Customer';
}else{
$roles = $user->roles;
if (!empty($roles) && is_array($roles)) {
$roles = array_map("wecpFormatUserRole",$roles);
$user_role = implode(', ',$roles);
}
}
}
$short_codes['customer']['role'] = $user_role;
}
return $short_codes;
}
function wecpFormatUserRole($role){
return str_replace('_', ' ', ucfirst($role));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment