微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

隐藏自定义结帐字段,以免出现在 WooCommerce 中我的帐户编辑地址

如何解决隐藏自定义结帐字段,以免出现在 WooCommerce 中我的帐户编辑地址

我使用 wooocmmerec 结帐字段编辑器插件创建了一个 woo-commerce 结帐字段,如下图

enter image description here

应该在结帐时有条件地出现,这里是启用它的代码

function ga_checkout_fields($fields) {
    global $user_country;
    //$user_country = 'in'; //for test in dev

    if( $user_country != 'in')  { //hide gst if the location is not india
        unset($fields['billing']['billing_gst_number']);
    }
    
    if( $user_country != 'br')  { //hide taxid if the location is not brazil
        unset($fields['billing']['billing_br_tax_id']);
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields','ga_checkout_fields' );

代码在结帐时完美无缺,但问题是当用户尝试在我的帐户中编辑他们的帐单邮寄地址时,他们会看到这些自定义字段并且无法编辑他们的地址。我根本不想在客户的帐户编辑帐单地址表单中向客户显示或保存自定义字段。 我尝试使用以下代码在我的帐户中的帐单地址处取消设置这些自定义字段,我可以隐藏这些字段但无法保存它们,因为在结帐时需要税号字段。我希望在结帐时需要税号,并且我不想在我的帐户中的编辑帐单地址中有任何自定义字段?关于如何实现这一目标的任何见解?

function ga_remove_the_address_field( $address,$load_address ) {//this removes fields in the edit form
    global $user_country;
 
    if( $user_country != 'in')  { //hide gst if the location is not india
        unset($address['billing_gst_number']);
    }
    
    if( $user_country != 'br')  { //hide taxid if the location is not brazil
        unset($address['billing_br_tax_id']);
    }

    return $address;

}
 
add_filter( 'woocommerce_address_to_edit','ga_remove_the_address_field',10,2 );

解决方法

要在我的帐户中隐藏自定义字段 > 编辑帐单邮寄地址,您将使用以下内容:

add_filter( 'woocommerce_billing_fields','ga_remove_account_custom_fields',1000 );
function ga_remove_account_custom_fields( $fields ) {
    // Only on My account > Edit address section
    if ( is_wc_endpoint_url('edit-address') ) {
        // Here define the custom field Ids to hide in the array
        $field_ids = array('billing_gst_number','billing_br_tax_id');

        foreach ( $field_ids as $field_id ) {
            if( isset($fields[$field_id]) ) {
                unset($fields[$field_id]);
            }
        }
    }
    return $fields;
}

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

注意:对于运输字段,您将使用挂钩 woocommerce_shipping_fields


相关:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。