简体   繁体   English

在 WooCommerce 中将字段设为可选我的帐户编辑地址

[英]Make fields optional in WooCommerce My account edit addresses

How to make all fields optional in WooCommerce My account edit addresses only?如何在 WooCommerce 我的帐户中仅编辑地址中的所有字段都是可选的?

How to disable the required notice:如何禁用所需的通知:

需要通知

And the required html:以及所需的 html:

必需的 html

How to remove required?需要怎么去除? Can you help me?你能帮助我吗?

To make all My account > addresses fields optional use the following:要使所有我的帐户 > 地址字段可选,请使用以下内容:

// Make all My account addresses fields optional
add_filter( 'woocommerce_default_address_fields' , 'filter_my_account_addresses_fields', 999 );
add_filter( 'woocommerce_billing_fields', 'filter_my_account_addresses_fields', 999 );
function filter_my_account_addresses_fields( $fields ) {
    // Only on My account edit addresses
    if ( is_wc_endpoint_url( 'edit-address' ) ) {
        // Loop through existing fields
        foreach( $fields as $field_key => $field_data ) {
            // if they are required
            if( $fields[$field_key]['required'] ) {
                // Make them optional
                $fields[$field_key]['required'] = false;
            }
        }
    }

    return $fields;
}

// Optionaly remove ("optional)" text from My account addresses fields
add_filter( 'woocommerce_form_field' , 'remove_account_addresses_optional_fields_label', 10, 4 );
function remove_account_addresses_optional_fields_label( $field, $key, $args, $value ) {
    // Only on My account edit addresses
    if ( is_wc_endpoint_url( 'edit-address' ) ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce My account 和 Checkout 上自定义地址字段 - Customize addresses fields on WooCommerce My account and Checkout 在Woocommerce 3中自定义我的帐户地址字段 - Customizing my-account addresses fields in Woocommerce 3 从 Woocommerce 3.4+ 中我的帐户编辑地址字段中删除(可选)文本 - Remove (optional) text from fields on My account edit address in Woocommerce 3.4+ 自定义Woocommerce模板my-account / form-edit-addresses - Customizing Woocommerce template my-account/form-edit-addresses 禁用或只读 Woocommerce 中编辑帐户页面中的字段 - Disable or make read only the fields from edit account pages in Woocommerce WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's edit page Woocommerce:验证我的帐户编辑页面上的自定义字段 - Woocommerce: validate custom fields on my account edit page 使WooCommerce中不需要结帐地址字段 - Make checkout addresses fields not required in WooCommerce 从WooCommerce中我的帐户编辑地址中删除计费电话和电子邮件字段 - Remove billing phone and email fields from my account edit address in WooCommerce 隐藏自定义结帐字段不出现在 WooCommerce 我的帐户编辑地址 - Hide custom checkout fields from appearing in WooCommerce my account edit address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM