【问题标题】:WooCommerce - Checkout conditional fields for different persons custom statusWooCommerce - 结帐不同人自定义状态的条件字段
【发布时间】:2016-08-16 10:54:41
【问题描述】:

我需要修改 woocommere 网站的结帐流程。该过程取决于可能是其中之一的人的状态:
- 'legal entity'
- 'individual'

我需要一个选择器'User status' (或单选按钮),就在“billing_first_name”和“billing_last_name”之后。

选择器应该这样工作:

  1. 如果选择'legal entity',它应该显示3个字段:
    • '电话号码'
    • '电子邮件'
    • 'serial_id'
  2. 如果选择'individual',它应该显示其他3个字段:
    • 'custom_field1'
    • 'custom_field2'
    • 'custom_field3'

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

我怎样才能做到这一点?

谢谢。

【问题讨论】:

  • 用户将完成姓氏之后。结构如下: - 名字 - 姓氏 - 个人身份 a) 法人实体:b) 个人,如果是 a),将显示:电话号码、电子邮件和序列号,如果是 b) 将出现:其他 3 个字段,然后是经典字段。
  • 由于您的问题太宽泛,我只会在您拆分问题时提供帮助:第 1 部分(现在)制作新的结帐自定义帐单字段并重新排序帐单字段(以匹配您想要的内容)......然后(之后)关于另一个问题:制作条件部分(需要jQuery(javascript)和可能的ajax)。所以请编辑你的问题只有第 1 部分
  • 我需要修改 woocommere 网站的结帐流程。该过程由可能是以下人员之一的人的状态决定: - '法人实体' - '个人' 我需要有一个选择器'用户状态'(或单选按钮),就在'billing_first_name'和' billing_last_name'。选择器应该这样工作:如果选择了“法人实体”,它应该显示 3 个字段:“电话号码”“电子邮件”“序列号”
  • 我已经理解您的问题(无需解释),但是您的问题对于一个问题来说太宽泛了……请再读一遍我的第一条评论……如果您同意,我将只在此处发布第 1 部分。在此之后,使用我的答案代码,您将在第 2 部分的新线程上提出另一个问题……您是否同意……如果您同意,请在此处发表评论,并带有“我同意拆分我的问题”。完成后,我将在这里回答第 1 部分……
  • oo,我现在明白了,我以为你想发布另一个问题.. 当然,我同意拆分我的问题

标签: php wordpress woocommerce checkout custom-fields


【解决方案1】:

对于 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;

参考:Reordering checkout fields in 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 文件中

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

现在有了这段代码,你可以提出一个问题来处理这个问题的“条件”部分(正如我在 cmets 中告诉你的那样)......

官方参考:WooThemes - Customizing checkout fields using actions and filters

【讨论】:

  • 好的,我按照你说的做了,接下来我该怎么做?我不明白的是,wocommerce 已经有电话号码和电子邮件。我不需要添加其他字段.. 可以吗?
  • @BursucAndrei 我的代码没有创建电子邮件和电话。我刚刚重新订购了现有的电话和电子邮件。现在使用我的答案代码,您可以创建新问题,说您有此代码用于创建/订购结帐字段并且您希望拥有(问题的第二部分)。完成后,请在此处发布链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 2023-04-10
  • 2018-07-21
  • 2018-08-07
  • 2017-03-21
  • 2013-07-07
相关资源
最近更新 更多