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

时间:2020-04-26 12:46:20

标签: php wordpress templates woocommerce email-notifications

我使用代码让用户选择注册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 个答案:

答案 0 :(得分:0)

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

新代码位于 NEW PART END NEW PART 之间,其余代码为 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 );