gpt4 book ai didi

php - 在 WooCommerce 帐户激活邮件中访问用户的名字(DOI 通过 WooCommerce Germanized)

转载 作者:行者123 更新时间:2023-12-03 11:21:50 26 4
gpt4 key购买 nike

我们的 WooCommerce 网站有一个自定义注册页面,该页面基于 Business Bloomer 短代码,并为用户的名字添加了一个输入字段。我们的functions.php 中的代码如下所示:

/**
* @snippet WooCommerce User Registration Shortcode
* @author Rodolfo Melogli
* @compatible WooCommerce 3.6.5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/

// THIS WILL CREATE A NEW SHORTCODE: [wc_reg_form_bbloomer]
add_shortcode('wc_reg_form_bbloomer', 'bbloomer_separate_registration_form');

function bbloomer_separate_registration_form()
{
if (is_admin()) return;
if (is_user_logged_in()) return;
ob_start();

// NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommerce\templates\myaccount\form-login.php
// IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY

?>

<form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action('woocommerce_register_form_tag'); ?> >

<?php do_action('woocommerce_register_form_start'); ?>

<?php if ('no' === get_option('woocommerce_registration_generate_username')): ?>

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_username"><?php esc_html_e('Username', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo (!empty($_POST['username'])) ? esc_attr(wp_unslash($_POST['username'])) : ''; ?>" /><?php // @codingStandardsIgnoreLine
?>
</p>

<?php
endif; ?>

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?php esc_html_e('Email address', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo (!empty($_POST['email'])) ? esc_attr(wp_unslash($_POST['email'])) : ''; ?>" /><?php // @codingStandardsIgnoreLine
?>
</p>

<?php if ('no' === get_option('woocommerce_registration_generate_password')): ?>

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password"><?php esc_html_e('Password', 'woocommerce'); ?>&nbsp;<span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
</p>

<?php
else: ?>

<p><?php esc_html_e('A password will be sent to your email address.', 'woocommerce'); ?></p>

<?php
endif; ?>

<?php do_action('woocommerce_register_form'); ?>

<p class="woocommerce-FormRow form-row">
<?php wp_nonce_field('woocommerce-register', 'woocommerce-register-nonce'); ?>
<button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e('Register', 'woocommerce'); ?>"><?php esc_html_e('Register', 'woocommerce'); ?></button>
</p>

<?php do_action('woocommerce_register_form_end'); ?>

</form>

<?php
return ob_get_clean();
}

/**
* @snippet Add First to Register Form - WooCommerce
* @sourcecode https://businessbloomer.com/?p=21974
* @author Rodolfo Melogli
* @credits Claudio SM Web
* @compatible WC 3.5.2
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/

///////////////////////////////
// 1. ADD FIELDS

add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );

function bbloomer_add_name_woo_account_registration() {
?>

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>

<div class="clear"></div>

<?php
}

///////////////////////////////
// 2. VALIDATE FIELDS

add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );

function bbloomer_validate_name_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Achtung</strong>: Vorname ist ein Pflichtfeld!', 'woocommerce' ) );
}
return $errors;
}

///////////////////////////////
// 3. SAVE FIELDS

add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );

function bbloomer_save_name_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
}

}

由于我们的网站位于德国,因此我们使用具有 DOI(双重选择加入)功能的 WooCommerce 德国化插件。因此,用户会收到一封激活邮件,他们必须点击该邮件才能激活帐户。在激活邮件中,我们希望有更个性化的感觉,因此包含用户的名字。在那里,我们将来自 WooCommerce Germanized 的原始电子邮件模板添加到我们的子主题中。

问题是我可以访问的唯一值(显示在实际发送的电子邮件中)是 $user_login 它返回自动创建的用户名。如何访问保存为名字和帐单名字的用户的名字。我们子主题中的模板如下所示:
<?php
/**
* Customer new account activation email.
*
* @see https://github.com/vendidero/woocommerce-germanized/wiki/Overriding-Germanized-Templates
* @package Germanized/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

?>

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

<p><?php printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $user_login ) ); ?></p>

<p><?php printf( __( "Thanks for creating an account on %s. Please follow the activation link to activate your account:", 'woocommerce-germanized' ), esc_html( $blogname ) ); ?></p>

<p><a class="wc-button button"
href="<?php echo esc_url( $user_activation_url ); ?>"><?php _e( 'Activate your account', 'woocommerce-germanized' ); ?></a>
</p>

<?php if ( get_option( 'woocommerce_registration_generate_password' ) == 'yes' && $password_generated ) : ?>

<p><?php printf( __( "Your password has been automatically generated: <strong>%s</strong>", 'woocommerce-germanized' ), esc_html( $user_pass ) ); ?></p>

<?php endif; ?>

<p style="font-size:75%;"><?php printf( __( "If you haven't created an account on %s please ignore this email.", "woocommerce-germanized" ), esc_html( $blogname ) ); ?></p>

<p style="font-size:75%;"><?php printf( __( 'If you cannot follow the link above please copy this url and paste it to your browser bar: %s', 'woocommerce-germanized' ), esc_url( $user_activation_url ) ); ?></p>

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

<?php do_action( 'woocommerce_email_footer', $email ); ?>

我已经尝试填充此处描述的名字( https://wordpress.org/support/topic/new-account-email-insert-first-name/ )并通过 $user->display_name 访问它,但这不起作用。

我还尝试了此处提供的解决方案 ( Get user meta data from woocommerce_created_customer hook in Woocommerce ) 通过 Customer 对象访问名字。代码如下所示。我也没工作。
<p><?php $customer = new WC_Customer( $customer_id ); printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $customer->get_first_name() ) ); ?></p>

任何帮助将不胜感激!

最佳答案

首先你需要找到用户。我假设您无权访问用户 ID,如果可以,您可以跳过第一行,并在适当的位置插入 user_id。

然后你需要得到名字from the meta data .根据您的代码,您将其存储在元数据字段 'billing_first_name' 中.然后就是将它插入到您的电子邮件中。

<?php 

$the_user = get_user_by('login', $user_login);
$first_name = get_user_meta($the_user->ID, 'billing_first_name', true);

?>
<p><?php printf( __( 'Hi %s,', 'woocommerce' ), esc_html( $first_name ) ); ?></p>

关于php - 在 WooCommerce 帐户激活邮件中访问用户的名字(DOI 通过 WooCommerce Germanized),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60062120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com