Created
September 24, 2020 05:57
-
-
Save iamsathyaseelan/bd653ccf955ca88225a149b466bafdd8 to your computer and use it in GitHub Desktop.
email customizer plus - get customer role shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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