移除WooCommerce结账字段的值

7

我试图在我的WooCommerce结账表单中应用autocomplete="off",但它就是无法生效。

有没有方法可以让结账表单实现自动填充关闭?

我查看了文档,但没有可用的信息。我还尝试将默认值设置为空,但也不起作用。当然,这是在用户未登录时。

enter image description here

更新:

我尝试了@smvax建议的unset,但它也没有起作用。

add_filter('woocommerce_checkout_fields', 'default_values_checkout_fields');
function default_values_checkout_fields($fields) {
  if (!is_user_logged_in()) {
      unset($fields['billing_city']);
      unset($fields['billing_first_name']);
      unset($fields['billing_last_name']);
      unset($fields['billing_company']);
      unset($fields['billing_address_1']);
      unset($fields['billing_address_2']);
      unset($fields['billing_city']);
      unset($fields['billing_postcode']);
      unset($fields['billing_country']);
      unset($fields['billing_state']);
      unset($fields['billing_email']);
      unset($fields['billing_phone']);
      unset($fields['shipping_city']);
      unset($fields['shipping_first_name']);
      unset($fields['shipping_last_name']);
      unset($fields['shipping_company']);
      unset($fields['shipping_address_1']);
      unset($fields['shipping_address_2']);
      unset($fields['shipping_postcode']);
      unset($fields['shipping_country']);
      unset($fields['shipping_state']);
      return $fields;
  }
}

我也尝试了答案这里,但它也没有起作用。
谢谢。

@smzvax 我刚刚尝试了 unset,整个表单都消失了。 - elimariaaa
在这里添加 [‘clear’] 怎么样:https://surpriseazwebservices.com/edit-woocommerce-checkout-fields/ - smzapp
@jeepers_creepers,您是在哪里添加AUTOCOMPLETE =“off”属性的? - Mayank Raj
@MayankRaj 在 <form> - elimariaaa
@smzvax 也不起作用 :( - elimariaaa
显示剩余3条评论
2个回答

17
你可以使用'__return_empty_string'函数和woocommerce_checkout_get_value WooCommerce过滤器钩子来获取空值,方法如下:
add_filter('woocommerce_checkout_get_value','__return_empty_string', 1, 1);

当结账页面加载时,这将清空所有结账值。 代码放在您活动的子主题(或主题)或任何插件文件的functions.php文件中。 此代码经过测试并可行。
您应该在Woocommerce设置>系统状态>工具中重置所有现有会话,以及如果您的电子商务使用任何缓存,则应该清除缓存。还要清空浏览器缓存和存储的数据。
相关链接:在Woocommerce中仅清除某些结帐字段值

谢谢您的回答,但是我尝试过后它没有起作用。我把它添加到了我的子主题的functions.php文件中。我的结账网址:http://dfp-dev.creativequoin.com/checkout/?v=4e45c2af0995 - elimariaaa
@jeepers_creepers 我已在3个不同的WooCommerce网站上测试了这段代码,并且它在所有网站上都可以正常工作。因此,肯定是您的主题或第三方插件中存在一些自定义代码导致冲突...我已更改了我的钩子(代码)中的优先级。请再试一次。我希望它能像应该的那样工作。您可以在我的原始测试服务器上进行测试:http://www.cbleu.net/sites/tie/shop/ (购买任何商品,然后在第一笔订单之后,添加一些商品到购物车并进入结账页面...您将看不到结账字段中的任何数据)。 - LoicTheAztec
当我将其添加到我的自定义WooCommerce插件中时,它可以工作。谢谢! - elimariaaa

0
您可以使用 woocommerce_checkout_fields 过滤器为表单字段设置 autocomplete="off"。
    add_filter('woocommerce_checkout_fields', 'autocomplete_off_checkout_fields');
    function autocomplete_off_checkout_fields($fields) {
          $fields['billing_first_name']['autocomplete'] = 'off';
          return $fields;
      }
    }

这将适用于所有的WooCommerce结账字段。


请注意,Chrome浏览器会有意忽略“off”值,并根据用户在Chrome中添加的内容(例如付款数据或地址)仍然应用自动完成功能。 - Sven

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接