【问题标题】:Woocommerce - Remove fields from woocommerce_form_field including validationWoocommerce - 从 woocommerce_form_field 中删除字段,包括验证
【发布时间】:2021-07-01 07:42:05
【问题描述】:

我正在尝试自定义 Woocommerce myaccount 页面,尤其是编辑地址页面。

我想在一个页面上同时显示送货地址和帐单地址表单。理想情况下,在带有一个保存按钮的单一表单中。我还需要删除很多字段,以便它是一种更简单的地址形式(没有姓名、公司等)。

我已经实现了This Answer 上的代码。它工作得很好,因为它显示了两种形式。但是,我无法从表单中删除这些字段。如果我尝试这样的代码:

add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );


function custom_override_billing_fields( $fields ) {
    unset($fields['billing_country']);
    unset($fields['billing_company']);
    unset($fields['billing_first_name']);
    unset($fields['billing_last_name']);
    unset($fields['billing_phone']);
    unset($fields['billing_email']);
  return $fields;
}

function custom_override_shipping_fields( $fields ) {
    unset($fields['shipping_country']);
    unset($fields['shipping_company']);
    unset($fields['shipping_first_name']);
    unset($fields['shipping_last_name']);
  return $fields;
}

它不起作用,不再显示字段,但点击时表单不保存...它只是重定向到 /my-account/edit-address/billing/ - 并且不保存。 (此页面上显示的相同表单也不保存)。

我也试过了:

foreach ( $billing_fields as $key => $field ) :
   if($key != 'billing_first_name' && $key != 'billing_last_name') :
      woocommerce_form_field( $key, $field, $userMeta[$key][0] );
   endif;
endforeach;

这会从显示中删除该字段,但验证仍然存在 - 以及我添加到函数中使用的任何过滤器代码 woocommerce_checkout_fields 删除验证似乎根本不会影响此表单。

有没有办法:

  1. 从此表单中删除由 woocommerce_form_field 生成的字段,包括验证?
  2. 创建一个自定义表单,允许我在代码中手动设置输入字段,并更新其中的任何字段,完全忽略来自 Woocommerce 的验证?

【问题讨论】:

    标签: wordpress woocommerce filter


    【解决方案1】:

    这应该 100% 有效。您需要说明要删除的字段是来自计费还是运输,这可以通过添加 ['billing'] 或 ['shipping'] 来完成,无论是哪个。

    在此之后,直接将功能添加到 woocommerce_checkout_fields 将适用于计费和运输。

    对于电话和公司字段,您可以在管理面板本身中禁用它。

    编辑:是的,过去与字段相关的所有验证都将被删除。然后,您可以应用您需要的任何验证。

    add_filter( 'woocommerce_checkout_fields' , 'brandimagemarketer_remove_billing_fields_checkout' );
     
    function brandimagemarketer_remove_billing_fields_checkout( $fields ) {
      unset($fields['billing']['billing_country']);
      unset($fields['billing']['billing_first_name']);
     unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_email']);
    
      unset($fields['shipping']['shipping_country']);
      unset($fields['shipping']['shipping_first_name']);
     unset($fields['shipping']['shipping_last_name']);
    unset($fields['shipping']['shipping_email']);
    
      return $fields;
    }
    

    【讨论】:

    • 谢谢,但请重新阅读原帖。根据此答案,这些字段将显示在自定义 myaccount 地址页面上:stackoverflow.com/a/37286898/2115227。 - 您的代码不会从此代码中删除字段。我在原帖中添加的代码删除了字段,但没有保存。
    • 为了确认,表单正在“构建”: $field ) : ?>
    • 嗨,好的,知道了。我测试了你的代码,它对我来说很好,字段被删除,验证也被删除,提交后表单被保存,在我的帐户页面和结帐页面中没有任何错误通知。您的自定义代码或插件可能存在冲突。尝试评论您的自定义代码或禁用插件,看看是否有任何变化。
    • 我尝试了全新的 Wordpress 安装,运行 TwentyTwentyOne 主题,除了 Woocommerce 之外没有插件,除了我的原始帖子中的两个 add_filter 代码之外没有自定义代码......它没有保存到数据库。保存时,它看起来像是保存了地址,但是当我导航出去并返回到地址时,它没有保存
    • 排除 billing_country 时似乎没有保存。当我删除 billing_country 时,它会保存
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多