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

Posted

技术标签:

【中文标题】根据 Woocommerce 中的用户角色自定义客户新帐户邮件【英文标题】:Customize customer new account mail based on user role in Woocommerce 【发布时间】:2020-08-09 22:53:50 【问题描述】:

我使用代码让用户在注册 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 );

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

【问题讨论】:

【参考方案1】:

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

新代码位于NEW PARTEND NEW PART之间,其余为customer-new-account.php模板文件中的标准代码。 p>

<?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 中的用户角色自定义客户新帐户邮件的主要内容,如果未能解决你的问题,请参考以下文章

为 Woocommerce 中的特定用户角色自定义我的帐户新菜单项

根据 Woocommerce 中的用户角色更改 COD 支付网关的默认订单状态

WooCommerce 根据用户角色更改 BACS 订单状态

在 WooCommerce 中隐藏特定用户角色的增值税

根据 WooCommerce 中的运输类别自定义计算的运输成本

根据在 Woocommerce 中购买的总商品更改用户角色