php - WooCommerce - 不同人自定义状态的结帐条件字段

标签 php wordpress woocommerce checkout custom-fields

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

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

The selector should work this way:

  1. If 'legal entity' is selected, it should display 3 fields:
    • 'phone_number'
    • 'email'
    • 'serial_id'
  2. If 'individual' is selected, it should display 3 other fields:
    • 'custom_field1'
    • 'custom_field2'
    • 'custom_field3'

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

我怎样才能做到这一点?

谢谢。

最佳答案

对于 WooCommerce 3+ (更新):

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

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

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

## 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 Hook 。然后首先创建新字段。在自定义一些字段类之后。完成重新排序字段以满足您的需求。

代码如下:

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

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

Now with this code, yo can ask a question to handle the "conditional" part of this question (as I have tell you in comments)…

官方引用:WooThemes - Customizing checkout fields using actions and filters

关于php - WooCommerce - 不同人自定义状态的结帐条件字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38973024/

相关文章:

php - 在迷你购物车中显示 Woocommerce 订阅重复总数

php - paypal数字商品不显示选项 "buy as a guest"

php - PHP 中的依赖注入(inject)——简单示例回顾

php - WordPress如何动态生成页面?

php - Woocommerce:在前端使用元数据列出特定产品的销售情况

php - 更改 WooCommerce 购物车和结帐页面中的 'Shipping' 文本 :

mysql - 根据 Woocommerce 中的日期批量更改订单状态

php - 为什么第一次调用没有E_NOTICE错误?

php - 亚马逊 MWS : How to specify the language of _GET_MERCHANT_LISTINGS_DATA_

wordpress - 自定义帖子类型 slug 和永久链接问题 - SEO 和结构