在 Woocommerce 中禁用或只读编辑帐户页面中的字段

Disable or make read only the fields from edit account pages in Woocommerce

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

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

我该怎么做?

您可以通过将此添加到主题的 functions.php

轻松禁用 WooCommerce 帐户页面中的各个字段
function my_remove_checkout_fields($fields) {
    unset( $fields ['billing'] ['billing_first_name'] );
    unset( $fields ['billing'] ['billing_last_name'] );
    unset( $fields ['billing'] ['billing_email'] );

    // Any other fields you want to unset...

    return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'my_remove_checkout_fields' );

可以找到自定义字段的文档 here

更新

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

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

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

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


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

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。