在Woocommerce结帐中使结帐字段为必填项 [英] Make checkout fields required in Woocommerce checkout

查看:84
本文介绍了在Woocommerce结帐中使结帐字段为必填项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,帐单地址中的所有字段均标记为可选-客户将帐单地址字段保留为空白,然后拒绝付款(由我们的付款处理方Square处理)。

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).

我找不到在任何地方都必须填写这些字段,也无法弄清楚为什么在任何情况下都将其标记为可选字段。

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?

更新

我什至尝试了以下方法:

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

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). 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;
}

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

这篇关于在Woocommerce结帐中使结帐字段为必填项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆