简体   繁体   English

使 Woocommerce 结帐中的结帐字段成为必需

[英]Make checkout fields required in Woocommerce checkout

For some reason, all the fields in Billing Address are marked as optional - customers are leaving the billing address fields blank and then their payments are being rejected (by Square, who is our payment processor).出于某种原因,帐单地址中的所有字段都标记为可选 - 客户将帐单地址字段留空,然后他们的付款被拒绝(Square,我们的付款处理方)。

I cannot find anywhere to make these fields required, and cannot figure out why they would be marked as optional in any case.我找不到任何地方可以使这些字段成为必需,也无法弄清楚为什么在任何情况下它们都会被标记为可选。

Can someone point me in the right direction?有人可以指出我正确的方向吗?

UPDATE更新

I've even tried the following:我什至尝试过以下方法:

add_filter('woocommerce_billing_fields', 'force_billing_fields', 1000, 1);
function force_billing_fields($fields) {
  $fields['billing_first_name']['required'] = true;
  $fields['billing_last_name']['required'] = true;
  $fields['billing_address_1']['required'] = true;
  $fields['billing_city']['required'] = true;
  $fields['billing_postcode']['required'] = true;
  $fields['billing_country']['required'] = true;
  $fields['billing_state']['required'] = true;
  $fields['billing_email']['required'] = true;
  $fields['billing_phone']['required'] = true;

  return $fields;
}

And they're still marked as optional, except the billing phone and country are now marked as required.而且它们仍然被标记为可选,除了帐单电话和国家/地区现在被标记为必需。 But the rest are still optional.但其余的仍然是可选的。

What you can do if you don't find the guilty as explained on my comment is to use the following (using here a highest hook priority if some other code is already using those hooks) :如果您没有按照我的评论中的解释发现有罪,您可以做的是使用以下内容(如果其他代码已经在使用这些钩子,则在此处使用最高钩子优先级)

add_filter( 'woocommerce_default_address_fields', 'customising_checkout_fields', 1000, 1 );
function customising_checkout_fields( $address_fields ) {
    $address_fields['first_name']['required'] = true;
    $address_fields['last_name']['required'] = true;
    $address_fields['company']['required'] = true;
    $address_fields['country']['required'] = true;
    $address_fields['city']['required'] = true;
    $address_fields['state']['required'] = true;
    $address_fields['postcode']['required'] = true;

    return $address_fields;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 tested and works.测试和工作。

For billing phone and email you can try对于计费电话和电子邮件,您可以尝试

add_filter('woocommerce_billing_fields', 'custom_billing_fields', 1000, 1);
function custom_billing_fields( $fields ) {
    $fields['billing_email']['required'] = true;
    $fields['billing_phone']['required'] = true;

    return $fields;
}

or或者

add_filter('woocommerce_checkout_fields', 'custom_billing_fields', 1000, 1);
function custom_billing_fields( $fields ) {
    $fields['billing']['billing_email']['required'] = true;
    $fields['billing']['billing_phone']['required'] = true;

    return $fields;
}

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

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