如何删除所有Woocommerce结帐帐单字段而不会出现错误 [英] How to remove all Woocommerce checkout billing fields without errors

查看:74
本文介绍了如何删除所有Woocommerce结帐帐单字段而不会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该操作有助于使字段为非必填项

That action helps to make fields non-required

add_filter( 'woocommerce_checkout_fields', 'unrequire_checkout_fields' );
function unrequire_checkout_fields( $fields ) {
  $fields['billing']['billing_company']['required']   = false;
  $fields['billing']['billing_city']['required']      = false;
  $fields['billing']['billing_postcode']['required']  = false;
  $fields['billing']['billing_country']['required']   = false;
  $fields['billing']['billing_state']['required']     = false;
  $fields['billing']['billing_address_1']['required'] = false;
  $fields['billing']['billing_address_2']['required'] = false;
  return $fields;
}

但它仅适用于CSS

#billing_country_field, #billing_address_1_field, #billing_address_2_field,#billing_state_field,#billing_last_name_field,#billing_postcode_field,#billing_company_field {
        display: none !important; }

我认为这不是最好的决定.一定是这样

I thinks that's not the best decision. It has to be like that

add_filter('woocommerce_checkout_fields','remove_checkout_fields');
function remove_checkout_fields($fields){
    unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    return $fields;
}

但是,当我恰好添加该代码时,它需要地址并说pay方法(本地本地取件)是错误的.没有该代码,而使用CSS则一切正常.也许有人遇到过同样的问题并且已经解决了?如果可能的话,没有插件.

But when I'm adding exactly that code, it requires address and says that the pay method(native local pickup) is wrong. Without that code and with css all works fine. Maybe someone had the same problem and already solved it? Without plugins if possible.

推荐答案

这需要以不同的方式完成,因为WooCommerce用真实的字眼要求结帐帐单所在的国家/地区抛出诸如之类的错误.继续." :

This requires to be done in a different way, as in real word WooCommerce ask for checkout billing country throwing an error like "Please enter an address to continue.":

为避免此问题,请改用以下命令(您将在其中定义要删除的所有关键字段):

To avoid this problem use the following instead (where you will define all key fields to be removed):

// Just hide woocommerce billing country
add_action( 'woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5 );
function hide_checkout_billing_country() {
    echo '<style>#billing_country_field{display:none;}</style>';
}

add_filter('woocommerce_billing_fields', 'customize_checkout_fields', 100 );
function customize_billing_fields( $fields ) {
    if ( is_checkout() ) {
        // HERE set the required key fields below
        $chosen_fields = array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'postcode', 'country', 'state');

        foreach( $chosen_fields as $key ) {
            if( isset($fields['billing_'.$key]) && $key !== 'country') {
                unset($fields['billing_'.$key]); // Remove all define fields except country
            }
        }
    }
    return $fields;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

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

这篇关于如何删除所有Woocommerce结帐帐单字段而不会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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