根据 Woocommerce 中的用户角色自定义客户新帐户邮件 [英] Customize customer new account mail based on user role in Woocommerce

查看:24
本文介绍了根据 Woocommerce 中的用户角色自定义客户新帐户邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用代码让用户在注册 WooCommerce 时选择要使用的角色.

I use code to let users choose which role to use when registering for WooCommerce.

这是因为我有两种人:私人和专业.

This is because I have 2 types of people: private and professional.

我想根据注册时选择的角色发送不同的电子邮件.

What I would like is to send a different email according to the role selected in the registration.

以下代码允许新用户在注册时立即选择用户角色

The following code allows a new user to immediately choose a user role during registration

/* Add Role Choose Option on Registration */
//adding role select option on registration form
function wooc_extra_register_fields() {
global $wp_roles; ?>
   <p class="form-row form-row-wide">
   <label for="reg_billing_phone">Select Role</label>
   <select name="role" class="input">
    <?php 
      foreach ( $wp_roles->roles as $key=>$value ) {
      // Exclude default roles such as administrator etc. Add your own
      if ( ! in_array( $value['name'], [ 'Administrator', 'Author', 'Editor', 'Shop Manager' ] ) ){
      echo '<option value="'.$key.'">'.$value['name'].'</option>';
       }
      }
    ?>
  </select>
   </p>
    <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

//saving role
add_action( 'woocommerce_created_customer', 'update_user_role' );
function update_user_role( $user_id ) {
$user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
}

<小时>

此代码是一个修改过的woocommerce/emails/customer-new-account.php 模板文件.

<?php
/**
* Customer new account email
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/customer- 
new-account.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates/Emails
* @version 3.7.0
*/

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php /* translators: %s: Customer username */ ?>

<!--UTENTE REGISTRATO COME CLIENTE PROFESSIONISTA-->

<?php if( current_user_can('professionista')): ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>
<?php /* translators: %1$s: Site title, %2$s: Username, %3$s: My account link */ ?>
<p><?php printf( esc_html__( 'PROFESSIONISTA Thanks for creating an account on %1$s. Your 
username is %2$s. You can access your account area to view orders, change your password, and 
more at: %3$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_login ) 
. '</strong>', make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); // 
phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>

<?php else: ?>

<!--UTENTE REGISTRATO COME CLIENTE NORMALE-->

<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>
<?php /* translators: %1$s: Site title, %2$s: Username, %3$s: My account link */ ?>
<p><?php printf( esc_html__( 'NORMALE Thanks for creating an account on %1$s. Your username 
is %2$s. You can access your account area to view orders, change your password, and more at: 
%3$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_login ) . 
'</strong>', make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); // 
phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php endif; ?>

<?php if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && 
$password_generated ) : ?>
<?php /* translators: %s: Auto generated password */ ?>
<p><?php printf( esc_html__( 'Your password has been automatically generated: %s', 
'woocommerce' ), '<strong>' . esc_html( $user_pass ) . '</strong>' ); ?></p>
<?php endif; ?>

<?php
/**
* Show user-defined additional content - this is set in each email's settings.
*/
if ( $additional_content ) {
echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

do_action( 'woocommerce_email_footer', $email );

电子邮件已发送,但未进入专业角色的条件.

推荐答案

您是否测试过 current_user_can(); 在电子邮件模板中是否按照您的使用方式工作?

Have you tested whether current_user_can(); works inside in the email template the way you use it?

新代码介于 NEW PART &结束新部分,其余的是来自customer-new-account.php 模板文件.

New code is between NEW PART & END NEW PART, the rest is standard code from the customer-new-account.php template file.

<?php
/**
 * Customer new account email
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce/Templates/Emails
 * @version 3.7.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<!-- NEW PART -->

<?php
// Get user role(s)
$roles = $email->object->roles;

if ( in_array( 'administrator', $roles ) ) {
    echo 'administrator';
} elseif ( in_array( 'professionista', $roles ) ) {
    echo 'professionista';
} else {
    echo 'other user role';
}
?>

<!-- END NEW PART -->

<?php /* translators: %s: Customer username */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>
<?php /* translators: %1$s: Site title, %2$s: Username, %3$s: My account link */ ?>
<p><?php printf( esc_html__( 'Thanks for creating an account on %1$s. Your username is %2$s. You can access your account area to view orders, change your password, and more at: %3$s', 'woocommerce' ), esc_html( $blogname ), '<strong>' . esc_html( $user_login ) . '</strong>', make_clickable( esc_url( wc_get_page_permalink( 'myaccount' ) ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php if ( 'yes' === get_option( 'woocommerce_registration_generate_password' ) && $password_generated ) : ?>
    <?php /* translators: %s: Auto generated password */ ?>
    <p><?php printf( esc_html__( 'Your password has been automatically generated: %s', 'woocommerce' ), '<strong>' . esc_html( $user_pass ) . '</strong>' ); ?></p>
<?php endif; ?>

<?php
/**
 * Show user-defined additional content - this is set in each email's settings.
 */
if ( $additional_content ) {
    echo wp_kses_post( wpautop( wptexturize( $additional_content ) ) );
}

do_action( 'woocommerce_email_footer', $email );

这篇关于根据 Woocommerce 中的用户角色自定义客户新帐户邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆