首页 > 解决方案 > 将 WordPress 用户电子邮件而不是帐单电子邮件设置为 WooCommerce 结帐字段的默认值

问题描述

我想要做的是更改结帐字段,这样如果用户登录,他们就无法编辑他们的帐单电子邮件字段。

下面的代码很好用,但理想情况下,我想消除用户能够拥有 2 个电子邮件地址并将值设置$address_fields['billing']['billing_email']为用户帐户中的电子邮件地址的情况。

我试过添加

'value' => $current_user->user_email

'default' => $current_user->user_email

到数组但是这似乎什么都不做。

所以:

  1. 首先,我怎样才能为这个表单设置一个值并防止它被改变。
  2. 其次,是$current_user->user_email获取用户帐户(联系人)电子邮件而不是来自其帐户的帐单电子邮件的正确方法。

function custom_checkout_fields( $address_fields ) {
    
    
    $current_user = wp_get_current_user();
    
    if ( is_user_logged_in() ) {
        unset( $address_fields['billing']['billing_email'] );
        unset( $address_fields['billing']['billing_em_ver'] );
    
    $address_fields['billing']['billing_email'] = [
            'label' => 'Email address',
            'required'  => false,
            'description'   =>  '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>',
            'custom_attributes' => [
                'disabled' => 'disabled',
            ]
        ];
    
    
    
        return $address_fields;
    } else{
    return $address_fields;
    }
    }
    add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' ,20, 1 );

标签: phpwordpresswoocommercehook-woocommerce

解决方案


首先,转到/plugins/woocommerce/templates/checkout,复制form-billing.php在该文件夹中找到的模板文件并将其粘贴到yourTheme/woocommerce/checkout/.

form-billing.php其次,通过编辑复制的模板文件并更改以下代码段来防止 WooCommerce 自动填充电子邮件地址字段:

    <div class="woocommerce-billing-fields__field-wrapper">
      <?php
      $fields = $checkout->get_checkout_fields( 'billing' );

      foreach ( $fields as $key => $field ) {
        woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
      }
      ?>
    </div>

对此:

    <div class="woocommerce-billing-fields__field-wrapper">
      <?php
      $fields = $checkout->get_checkout_fields( 'billing' );

      foreach ( $fields as $key => $field ) {
        if ( 'billing_email' != $key ) {
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
        } else {
            woocommerce_form_field( $key, $field );
        }
      }
      ?>
    </div>

第三,将 WordPress 电子邮件注入该字段,使其不可变,并通过将以下内容添加到您的主题functions.php文件中来添加您的自定义描述:

function shillongtitude_billing_email_field($fields) {
  
  if ( is_user_logged_in() ) {

    $current_user = wp_get_current_user();

    //set the user email as the email field value
    $fields['billing']['billing_email']['default'] = $current_user->user_email;
    
    //set the description
    $fields['billing']['billing_email']['description'] = '<i><a href="/shop/my-account/customer-logout/">to change this please click here logout and login/register as another user</a> or <a href="/shop/my-account/edit-account/">click here to modify the address on your account</a></i>';
    
    // set attributes
    $fields['billing']['billing_email']['custom_attributes'] = array( 'readonly'=>'readonly', 'label' => 'Email address', 'required' => false );
            
    return $fields;
  } else {
    return $fields;
  }
}
add_filter('woocommerce_checkout_fields', 'shillongtitude_billing_email_field');


推荐阅读