沃梦达 / 编程问答 / php问题 / 正文

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

Make checkout fields required in Woocommerce checkout(使 Woocommerce 结帐中的结帐字段成为必需)

本文介绍了使 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 结帐中的结帐字段成为必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使 Woocommerce 结帐中的结帐字段成为必需

基础教程推荐