WooCommerce-不同人自定义状态的结帐条件字段 [英] WooCommerce - Checkout conditional fields for different persons custom status

查看:65
本文介绍了WooCommerce-不同人自定义状态的结帐条件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改woocommere网站的结帐流程。该过程由以下人员的身份决定:

- '法律实体' < br>
- 个人

I need to modify the checkout process for an woocommere website. The process is determinated by the status of the person that could be one of this:
- 'legal entity'
- 'individual'

我需要一个选择器 '用户状态' (或单选按钮),紧接在 billing_first_name和 billing_last_name之后。

I need to have a selector 'User status' (or radio buttons), just after the 'billing_first_name' and 'billing_last_name'.


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

The selector should work this way:


  1. 如果选择 '法律实体' ,则应显示3个字段:


    • 'phone_number'

    • 'email'

    • 'serial_id'

  1. If 'legal entity' is selected, it should display 3 fields:
    • 'phone_number'
    • 'email'
    • 'serial_id'

  • 'custom_field1'

  • 'custom_field2'

  • 'custom_field3'


我尝试了WooCommerce Check经理和另一个插件,但是问题是我无法满足匹配条件。

I tried WooCommerce Checkout Manager and another plugin but the problem is that I can't make matching conditions.

我该如何实现?

谢谢。

推荐答案

对于WooCommerce 3+ (更新)

For WooCommerce 3+ (update):


由于 WooCommerce 3.0结帐字段有所更改,因此无法像以前一样对字段进行重新排序。

Since WooCommerce 3.0 checkout fields have changed a little bit so is not possible to reorder fields as before.

有一个新的'priority'自变量,用于处理字段顺序,适用于结帐字段和我的帐户字段。

There is a new 'priority' argument that handle fields order, for checkout fields and my account fields as well.

下面,我仅更新与订购字段相关的部分:

Below I am just updating the part related to ordering fields:

## 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 钩子。然后,首先创建新字段。之后自定义一些字段类。要完成对字段的重新排序以符合您的需求。

You need to use woocommerce_checkout_fields hook. Then first create the new fields. After Customize some field classes. To finish reorder the field to match your desire.

以下是代码:

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文件中进行

Naturally this goes on function.php file of your active child theme or theme

此代码已经过测试且功能齐全。

This code is tested and fully functional.


现在有了此代码,您可以提出一个问题来处理有条件的这个问题的一部分(正如我在评论中告诉您的)…

官方参考: WooThemes-使用操作和过滤器来自定义结帐字段

这篇关于WooCommerce-不同人自定义状态的结帐条件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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