如何无错误地删除所有 Woocommerce 结帐账单字段

问题描述 投票:0回答:2

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

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

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

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

但是当我准确添加该代码时,它需要地址并表示付款方式(本地本地取货)是错误的。没有该代码并且使用 css 一切都可以正常工作。也许有人遇到了同样的问题并且已经解决了?如果可以的话不用插件。

php wordpress woocommerce hook-woocommerce woocommerce-checkout-fields
2个回答
6
投票

这需要以不同的方式完成,就像在现实中 WooCommerce 要求结帐帐单国家/地区抛出一个错误,如 “请输入地址以继续。”

为了避免此问题,请使用以下内容 (您将在其中定义要删除的所有关键字段)

// 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_billing_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 文件中。已测试并有效。


0
投票

这不起作用..我已经尝试了所有插件所有代码片段都不起作用..

我使用了这个,但它未能从我的结账页面中删除账单字段。 请修复我正在使用 worpress、woocommerce、tutorLMS、tutorstarter 主题 我的产品是虚拟数字产品,我真的不需要帐单地址 我几乎尝试了所有的东西..许多插件,代码片段.. 但没有一个起作用.. 我真的不明白为什么没有选项可以关闭数字产品的计费字段?

© www.soinside.com 2019 - 2024. All rights reserved.