【问题标题】:Woocommerce - Add custom checkout field to customer new account emailWoocommerce - 将自定义结帐字段添加到客户新帐户电子邮件
【发布时间】:2018-12-10 23:27:51
【问题描述】:

我正在尝试将我添加到结帐表单的“billing_gender”自定义字段显示到客户新帐户通知电子邮件中。

该字段已正确保存到数据库中,但在新帐户电子邮件中显示为空。

其他用户元字段(电话,...)有效,但我的自定义结帐字段无效。

我猜它被保存到用户元信息的后期,但我真的想不通:

这是我的代码:

functions.php

add_filter( 'woocommerce_checkout_fields' , 'divi_override_checkout_fields' );

function divi_override_checkout_fields( $fields ) {

unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_country']);

// Custom gender field
$fields['billing']['billing_gender'] = array(
    'type'     => 'select',
    'class'    => array( 'form-row-wide' ),
    'label'    => __( 'Title', 'divi-ultimate'),
    'required' => true,
    'priority' => 3,
    'options'  => array(
        ''           => __( 'Select title', 'divi-ultimate' ),
        'male'       => __( 'Mr', 'divi-ultimate' ),
        'female'     => __( 'Mrs', 'divi-ultimate' )
    ),
);

return $fields;
}

// Gender select default value

add_filter( 'default_checkout_billing_gender', 'checkout_billing_gender',10,2 );

 function checkout_billing_gender($value) {
if ( is_user_logged_in()){
    $current_user = wp_get_current_user();
    $value = get_user_meta( $current_user->ID, 'billing_gender', true );
}
return $value;
}


//* Update the order meta with fields values

 add_action('woocommerce_checkout_update_order_meta', 'divi_select_checkout_field_update_order_meta', 10, 2);

 function divi_select_checkout_field_update_order_meta( $order_id ) {

if ($_POST['delivery-shop']) update_post_meta( $order_id, 'delivery-shop', esc_attr($_POST['delivery-shop']));
if ($_POST['billing_gender']) update_post_meta( $order_id, 'billing_gender', esc_attr($_POST['billing_gender']));

 }

 //* Update the user meta with gender value

add_action( 'woocommerce_checkout_update_user_meta', 'divi_save_extra_user_fields', 10, 2 );

 function divi_save_extra_user_fields($customer_id) {
 if (isset($_POST['billing_gender'])) {
     update_user_meta( $customer_id, 'billing_gender',  esc_attr($_POST['billing_gender']) );
 }
 }

我做错了什么?

干杯。

【问题讨论】:

    标签: woocommerce custom-fields


    【解决方案1】:

    这是 customer-new-account.php 文件:

    $user = get_user_by('login', $user_login );
    
    if (!empty($user)) {
    $gender = get_user_meta($user->ID, 'billing_gender', true);
    $lastname = $user->last_name;
    $user_email = $user->user_email;
    }
    
    if( !empty($gender) && !empty($lastname) ) {
    printf( esc_html__( 'Dear ' . $gender . ' %s,', 'woocommerce' ), esc_html( $lastname ) );
    }
    else {
        printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $email ) );
    } ?>
    

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 2022-01-03
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多