简体   繁体   English

WooCommerce - 从我的帐户页面中删除国家/地区字段会阻止保存地址

[英]WooCommerce - removing country field from my-account page prevents from saving address

I sell and ship products only to one country so I don't want to display billing_country and shipping_country field to the customers.我只向一个国家销售和运送产品,因此我不想向客户显示billing_countryshipping_country字段。 I removed the fields from both checkout and my-account addresses page using unset($fields['billing_country']) .我使用unset($fields['billing_country'])从结帐和我的帐户地址页面中删除了这些字段

Checkout works fine, but after I removed those fields from my-account page, I can't save or edit any addresses on my-account addresses page.结帐工作正常,但在我从我的帐户页面中删除这些字段后,我无法保存或编辑我的帐户地址页面上的任何地址。 The page will simply refresh itself not changing anything.该页面将简单地刷新自身而不改变任何内容。

This is my functions.php inside my theme.这是我的函数。php 在我的主题中。

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
   unset($fields['billing']['billing_country']);
   unset($fields['shipping']['shipping_country']);
   unset($fields['billing']['billing_address_2']);
   unset($fields['shipping']['shipping_address_2']);
   unset($fields['shipping']['shipping_phone']);
   return $fields;
}

add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );

function custom_override_billing_fields( $fields ) {
  unset($fields['billing_country']);
  unset($fields['billing_address_2']);
        
  return $fields;
}

add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {
  unset($fields['shipping_country']);
  unset($fields['shipping_address_2']);
  unset($fields['shipping_phone']);
        
  return $fields;
}

If I comment out unset($fields['billing_country']);如果我注释掉unset($fields['billing_country']); or unset($fields['shipping_country']);unset($fields['shipping_country']); everything works fine and it saves any addresses.一切正常,它保存任何地址。 Unsetting other fields also works.取消设置其他字段也可以。 Am I doing something wrong, is there any other way to remove those fields?我做错了什么,有没有其他方法可以删除这些字段? Thanks.谢谢。

Update:更新:

WARNING - LONG STORY警告 - 长篇大论

The weirdest thing happened.最奇怪的事情发生了。 I made those fields hidden using css and left them set in functions.php.我使用 css 隐藏了这些字段,并将它们设置在 functions.php 中。 But then, my billing and shipping address classes got messed up!但是后来,我的帐单和送货地址类搞砸了!

I had those lines in my woocommerce_billing_fields filter hook:我的woocommerce_billing_fields过滤器挂钩中有这些行:

      $fields['billing_city']['class'] = array('form-row-last');
      $fields['billing_postcode']['class'] = array('form-row-first');
      $fields['billing_email']['class'] = array('form-row-last');
      $fields['billing_phone']['class'] = array('form-row-first');

This is supposed to make those 4 fields to take 50% width of the row in the billing address form.这应该使这 4 个字段占据帐单地址表单中行的 50% 宽度。 It worked with email and phone, but city and postcode remained 100% width .它适用于 email 和电话,但城市和邮政编码保持 100% 宽度 It should replace form-row-wide class into form-row-first/last and it did so, but only for two.它应该将form-row-wide class 替换为form-row-first/last并且它这样做了,但仅限于两个。

So I looked into the browser and found out that somehow those two elements had data-o_class attribute.所以我查看了浏览器,发现不知何故这两个元素具有data-o_class属性。

This is what I found:这是我发现的:

<p class="form-row validate-required validate-postcode form-row-wide address-field" 
id="billing_postcode_field" data-priority="65" 
data-o_class="form-row form-row-first validate-required validate-postcode"></p>

Element above is bugged and has this data-o_class attribute which contains all the classes, however, element below is fine and has his classes replaced:上面的元素有问题,并且具有包含所有类的data-o_class属性,但是,下面的元素很好,并且替换了他的类:

<p class="form-row form-row-first validate-required validate-phone" 
id="billing_phone_field" 
data-priority="100"></p>

When I removed the country field with unset($fields['billing_country']) , the problem disappeared, but again, I couldn't save the address.当我使用unset($fields['billing_country'])删除国家/地区字段时,问题消失了,但我又无法保存地址。

In order to fix it I had to use another filter - woocommerce_default_address_fields - This is what i did in functions.php :为了修复它,我不得不使用另一个过滤器 - woocommerce_default_address_fields - 这是我在functions.php中所做的:

  add_filter( 'woocommerce_default_address_fields','custom_override_default_address_fields' );
  function custom_override_default_address_fields( $fields ){
    unset($fields['country']);
    return $fields;
  }

What it did is even better!它所做的更好!

It made the country fields appear again both in checkout and on my account page, even though it was still unset with other filters there.它使国家/地区字段再次出现在结帐和我的帐户页面上,即使它仍然没有设置其他过滤器。 But it didn't appear in the right order, it was unstyled and at the end of the form.但它没有以正确的顺序出现,它没有样式并且位于表格的末尾。 So once again, I had to remove it using css .因此,我不得不再次使用css将其删除。

But.... It fixed the classes in my billing and shipping address on my account page.但是....它在我的帐户页面上修复了我的帐单和送货地址中的类。 I still don't know how this got my forms so messed up.我仍然不知道这如何让我的 forms 如此混乱。

You can get the same result without writing a line of code.无需编写任何代码即可获得相同的结果。

  1. In the Wordpress admin panel go to WooCommerce > Settings > General .在 Wordpress 管理面板 go 到WooCommerce >设置>常规
  2. In the field Selling location(s) select Sell to specific countries .在现场销售地点select销售到特定国家
  3. In the Sell to specific countries field, enter only the country in which you want to sell.Sell to specific countries字段中,仅输入您要销售的国家/地区。
  4. In the Shipping location(s) field select the option Ship to all countries you sell to .运送地点字段 select 中,选项运送到您销售到的所有国家/地区。

在此处输入图像描述

In this way, on the checkout page and on the address page in your account you will see the Billing country and Shipping country fields as plain text (instead of the list of options).这样,在您帐户的结帐页面和地址页面上,您将看到账单国家运输国家字段为纯文本(而不是选项列表)。

Customers will not be able to modify them.客户将无法修改它们。

If you want to hide it, however, you can intervene with the css code.但是,如果您想隐藏它,可以使用 css 代码进行干预。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM