繁体   English   中英

Woocommerce中根据用户角色自定义客户新账号邮件

[英]Customize customer new account mail based on user role in Woocommerce

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

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

我想要的是根据注册时选择的角色发送不同的email。


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

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

email 发送不进入职业角色状态

您是否测试过current_user_can(); 工程里面的email 模板你是怎么用的?

新代码在NEW PARTEND NEW PART之间,rest 是来自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 ); ?>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM