php - 将 WordPress 用户电子邮件而不是帐单电子邮件设置为 WooCommerce 结帐字段的默认值
问题描述
我想要做的是更改结帐字段,这样如果用户登录,他们就无法编辑他们的帐单电子邮件字段。
下面的代码很好用,但理想情况下,我想消除用户能够拥有 2 个电子邮件地址并将值设置$address_fields['billing']['billing_email']
为用户帐户中的电子邮件地址的情况。
我试过添加
'value' => $current_user->user_email
和
'default' => $current_user->user_email
到数组但是这似乎什么都不做。
所以:
- 首先,我怎样才能为这个表单设置一个值并防止它被改变。
- 其次,是
$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 );
解决方案
首先,转到/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');
推荐阅读
- wordpress - 编辑页面保存按钮未显示在 wordpress 中
- python - BeautifulSoup 找不到标签 3
- c# - 在html C#上显示特殊字符
- google-sheets - 在 Google 表格中按月求和
- c# - Excel 不愉快地显示大型 2D 范围公式数组
- javascript - 使用 sheetJS 在单个工作表中的多个表
- c++ - GNURADIO UHD 库链接器问题(C++)
- perl - 为什么 LWP::UserAgent 成功而 Mojo::UserAgent 失败?
- python - 将 Pandas 数据框写入 .txt 文件
- asp.net - Asp.net Core 2 域依赖视图选择?