根据 Woocommerce 中的用户角色自定义客户新帐户邮件

Customize customer new account mail based on user role in Woocommerce

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

这是因为我有两种类型的人:私人的和专业的。

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


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

/* 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(); 是否按照您使用的方式在电子邮件模板中工作?

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 );