WooCommerce - 检查不同人员自定义状态的结账条件字段

3
我需要修改一个woocommere网站的结账流程。该流程是由人员状态决定的,可能是以下之一:
- '法人实体'
- '个人'

我需要在“billing_first_name”和“billing_last_name”后面有一个选择器'用户状态' (或单选按钮)

选择器应该按照以下方式工作:

  1. 如果选择了 '法人实体',它应显示3个字段:
    • 'phone_number'
    • 'email'
    • 'serial_id'
  2. 如果选择了'个人',则应显示3个其他字段:
    • 'custom_field1'
    • 'custom_field2'
    • 'custom_field3'

我尝试过 WooCommerce Checkout Manager 和另一个插件,但问题是我无法创建匹配条件。

如何实现这一点?

谢谢。


用户完成姓氏后,将出现以下结构:
  • 名字
  • 姓氏
  • 个人状态 a) 法律实体: b) 个人
如果是 a),将显示电话号码、电子邮件和序列号如果是 b),将显示其他3个字段之后是经典字段。
- Panait Andrei Alexandru
2
由于您的问题太过宽泛,如果您将问题分成两部分,我可以提供帮助:第一部分(现在)是创建新的结账自定义账单字段和重新排序账单字段(以匹配您想要的内容)...然后(之后)在另一个问题中:制作条件部分(需要jQuery(javascript)和可能的ajax)。因此,请编辑您的问题,只保留第一部分。 - LoicTheAztec
我需要修改一个woocommere网站的结账流程。该流程由人员状态决定,可能是以下之一:
  • '法人实体'
  • '个人'
我需要在'billing_first_name'和'billing_last_name'之后有一个选择器'用户状态'(或单选按钮)。选择器应该按照以下方式工作:如果选择了“法人实体”,它应该显示3个字段: 'phone_number' 'email' 'serial_id'
- Panait Andrei Alexandru
我已经理解了你的问题(无需解释),但是你的问题对于一个问题来说太过宽泛了...请再次阅读我的第一条评论...如果你同意,我将只在这里发布第一部分的代码。之后,使用我的答案代码,你将在新的线程中提出另一个问题以获取第二部分的答案...你同意吗?如果同意,请在此处评论,“我同意拆分我的问题”。一旦完成,我将在此回答第一部分... - LoicTheAztec
哦,我明白了,我以为你想发另一个问题..当然,我同意分开提问。 - Panait Andrei Alexandru
1个回答

3

针对WooCommerce 3+ (更新):

由于WooCommerce 3.0结账字段有所更改,因此无法像以前那样重新排序字段。

现在有一个新的'priority'参数来处理结帐字段和我的帐户字段的顺序。

以下仅更新与字段排序相关的部分:

## 3. Ordering the billing fields

// Set the order of the fields
$billing_fields_new_order = array(
    'billing_first_name', 'billing_last_name', 'billing_status',
    'billing_email',      'billing_phone',     'billing_serial_id',
    'billing_custom1',    'billing_custom2',   'billing_custom3',
    'billing_company',    'billing_address_1', 'billing_address_2',
    'billing_postcode',   'billing_city',      'billing_country',
);

$count = 0;
$priority = 10;

// Updating the 'priority' argument
foreach($billing_fields_new_order as $field_name){
    $count++;
    $fields['billing'][$field_name]['priority'] = $count * $priority;
}

// END: returning the customized checkout fields
return $fields;

参考: 在WooCommerce 3中重新排序结算字段


原始回答:

你需要使用woocommerce_checkout_fields挂钩。然后首先创建新的字段。自定义一些字段类。最后重新排序字段以满足您的要求。

这是代码:

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_billing_fields' );
function custom_checkout_billing_fields( $fields ) {

// 1. Creating the additional custom billing fields

    // The "status" selector
    $fields['billing']['billing_status']['type'] = 'select';
    $fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
    $fields['billing']['billing_status']['required'] = true;
    $fields['billing']['billing_status']['label'] = __('User status', 'my_theme_slug');
    $fields['billing']['billing_status']['placeholder'] = __('Chose an option', 'my_theme_slug');
    $fields['billing']['billing_status']['options'] = array(
        '' => 'Chose an option',
        '1' => 'Legal entity',
        '2' => 'Individual'
    );

    // The "Serial ID" text field
    $fields['billing']['billing_serial_id']['type'] = 'text';
    $fields['billing']['billing_serial_id']['class'] = array('form-row-wide', 'status-group1');
    $fields['billing']['billing_serial_id']['required'] = true;
    $fields['billing']['billing_serial_id']['label'] = __('Serial ID', 'my_theme_slug');
    $fields['billing']['billing_serial_id']['placeholder'] = __('Enter your Serial ID', 'my_theme_slug');

    // The "Custom 1" text field
    $fields['billing']['billing_custom1']['type'] = 'text';
    $fields['billing']['billing_custom1']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom1']['required'] = true;
    $fields['billing']['billing_custom1']['label'] = __('Custom name 1', 'my_theme_slug');
    $fields['billing']['billing_custom1']['placeholder'] = __('Enter your custom1', 'my_theme_slug');

    // The "Custom 2" text field
    $fields['billing']['billing_custom2']['type'] = 'text';
    $fields['billing']['billing_custom2']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom2']['required'] = true;
    $fields['billing']['billing_custom2']['label'] = __('Custom name 2', 'my_theme_slug');
    $fields['billing']['billing_custom2']['placeholder'] = __('Enter your custom2', 'my_theme_slug');

    // The "Custom 3" text field
    $fields['billing']['billing_custom3']['type'] = 'text';
    $fields['billing']['billing_custom3']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_custom3']['required'] = true;
    $fields['billing']['billing_custom3']['label'] = __('Custom name 3', 'my_theme_slug');
    $fields['billing']['billing_custom3']['placeholder'] = __('Enter your custom3', 'my_theme_slug');


// 2. Customizing 'billing_email' and 'billing_phone' fields ['class']

    $fields['billing']['billing_email']['class'] = array('form-row-first', 'status-group1');
    $fields['billing']['billing_phone']['class'] = array('form-row-last', 'status-group1');


// 3. Ordering the billing fields

    $fields_order = array(
        'billing_first_name', 'billing_last_name', 'billing_status',
        'billing_email',      'billing_phone',     'billing_serial_id',
        'billing_custom1',    'billing_custom2',   'billing_custom3',
        'billing_company',    'billing_address_1', 'billing_address_2',
        'billing_postcode',   'billing_city',      'billing_country',
    );
    foreach($fields_order as $field) $ordered_fields[$field] = $fields['billing'][$field];

    $fields['billing'] = $ordered_fields;
    

// Returning Checkout customized billing fields

    return $fields;

}

自然而然,这段代码应该放在你的活动子主题或主题的function.php文件中。
此代码已经过测试并完全可用。
引用:
现在有了这个代码,您可以提出一个问题来处理这个问题的“条件”部分(正如我在评论中告诉您的那样)...
官方参考文献:WooThemes-使用操作和筛选器自定义结帐字段的教程

好的,我按照您的要求完成了,接下来我该做什么?我不明白的是,WooCommerce已经有电话号码和电子邮件了。我不需要再添加其他字段,这样可以吗? - Panait Andrei Alexandru
@BursucAndrei 我的代码没有创建电子邮件和电话。我只是重新排列了现有的电话和电子邮件。现在,使用我的答案代码,您可以创建新问题,说明您拥有此代码以创建/排序结帐字段,并且您想要(您问题的第二部分)。完成后,请在此处发布链接。 - LoicTheAztec

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