禁用或只读 Woocommerce 中编辑帐户页面中的字段 [英] Disable or make read only the fields from edit account pages in Woocommerce

查看:33
本文介绍了禁用或只读 Woocommerce 中编辑帐户页面中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,编辑帐户详细信息"页面具有以下字段:名字、姓氏、电子邮件地址、当前密码和新密码.

现在我只需要为客户用户禁用名字、姓氏、电子邮件地址字段.我正在使用 Flatsome WP 主题和 Woocommerce 插件.

我该怎么做?

解决方案

更新

<块引用>

对于编辑帐户详细信息"字段(电子邮件字段),您需要编辑 myaccount/form-edit-account.php 模板文件,因为这些字段是在模板中硬编码的

<块引用>

您必须将 readonly 属性添加到相关的输入字段中,例如在此提取示例中:

<块引用>

<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">

 

<块引用>

官方文档: 通过主题覆盖模板

对于我的帐户 >编辑帐单地址,以下代码将只读帐单字段的名字、姓氏和电子邮件:

add_filter( 'woocommerce_billing_fields', 'readonly_billing_account_fields', 25, 1 );函数 readonly_billing_account_fields ( $billing_fields ) {//只有登录用户的我的帐户帐单地址if( is_account_page() ){$readonly = ['readonly' =>'只读'];$billing_fields['billing_first_name']['custom_attributes'] = $readonly;$billing_fields['billing_last_name']['custom_attributes'] = $readonly;$billing_fields['billing_email']['custom_attributes'] = $readonly;}返回 $billing_fields;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

<块引用>

注意:要在结帐页面上也启用它,只需在 if(is_account_page()){ 和右括号 }代码>返回 $billing_fields;.


删除这 3 个字段,您将使用:

add_filter( 'woocommerce_billing_fields', 'remove_billing_account_fields', 25, 1 );函数 remove_billing_account_fields ( $billing_fields ) {//只有登录用户的我的帐户帐单地址if( is_account_page() ){未设置($billing_fields['billing_first_name']);未设置($billing_fields['billing_last_name']);未设置($billing_fields['billing_email']);}返回 $billing_fields;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Currently the Edit Account Details page have these fields: First Name, Last Name, Email address, Current Password & New Password.

Now I need to disable the First Name, Last Name, Email address fields for customers user only. I am using Flatsome WP theme and Woocommerce plugins.

How can I do this?

解决方案

Update

For edit "Account details" fields (email field), you will need to edit myaccount/form-edit-account.php template file as those fields are hard coded in the template

You will have to add a readonly attribute to the related input fields like in this extract example:

<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">

    <label for="account_first_name"><?php esc_html_e( 'First name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_first_name" id="account_first_name" autocomplete="given-name" value="<?php echo esc_attr( $user->first_name ); ?>" readonly />
</p>

Official documentation: Overriding templates via a theme

For My account > edit billing address, the following code will make readonly the billing fields first name, last name and email:

add_filter( 'woocommerce_billing_fields', 'readonly_billing_account_fields', 25, 1 );
function readonly_billing_account_fields ( $billing_fields ) {
    // Only my account billing address for logged in users
    if( is_account_page() ){

        $readonly = ['readonly' => 'readonly'];

        $billing_fields['billing_first_name']['custom_attributes'] = $readonly;
        $billing_fields['billing_last_name']['custom_attributes'] = $readonly;
        $billing_fields['billing_email']['custom_attributes'] = $readonly;
    }
    return $billing_fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Note: To enable that on checkout page too, Just remove if( is_account_page() ){ and the closing bracket } before return $billing_fields;.


To remove those 3 fields you will use:

add_filter( 'woocommerce_billing_fields', 'remove_billing_account_fields', 25, 1 );
function remove_billing_account_fields ( $billing_fields ) {
    // Only my account billing address for logged in users
    if( is_account_page() ){
        unset($billing_fields['billing_first_name']);
        unset($billing_fields['billing_last_name']);
        unset($billing_fields['billing_email']);
    }
    return $billing_fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于禁用或只读 Woocommerce 中编辑帐户页面中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆