WooCommerce - 为不同人员签出条件字段自定义状态

WooCommerce - 为不同人员签出条件字段自定义状态

问题描述:

我需要修改woocommere网站的结帐流程。该过程是由人的状态determinated这可能是这一个:
- 'legal entity'
- 'individual'WooCommerce - 为不同人员签出条件字段自定义状态

我需要有一个选择'User status'(或单选按钮),紧跟在'billing_first_name'和'billing_last_name'之后。

的选择应该以这种方式工作:

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

我试图WooCommerce Checkout Manager和另一个插件,但问题是我无法做出匹配条件纳秒。

我该如何做到这一点?

谢谢。

+0

。结构是这样的: - 名字 - 姓氏 - 人的状态 一)法人: 二)个人 如果它是一个),会显示出来:电话号码,电子邮件,以及串行ID 如果它会b)将显示:其他3个字段 和此后的经典领域。 –

+2

由于您的问题太宽泛,只有在您将问题分开的情况下,我才会提供帮助:第1部分(NOW)为新的结帐自定义结算字段和重新排序结算字段(以匹配您想要的结果)...然后(AFTER)处理其他问题:制作条件部分(需要jQuery(javascript)或者ajax)。所以请编辑您的问题只有第1部分 – LoicTheAztec

+0

我需要修改woocommere网站的结帐流程。这个过程是由这个人的状态决定的: - 'legal entity' - 'individual' 我需要一个选择器'用户状态'(或单选按钮),紧跟在' billing_first_name'和'billing_last_name'。 的选择应该以这种方式工作: 如果选择“法人”,它应该显示3个字段: “PHONE_NUMBER” “电子邮件” “serial_id” –

对于WooCommerce 3+ (更新)

由于WooCommerce 3.0结帐字段已经改变一点点所以是不可能的,因为之前重新排序字段。

有一个新的“优先”说法该处理场订购,用于结帐领域和我的帐户领域也是如此。

下面我只更新部分相关订货领域:

## 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文件。

现在有了这个代码,哟可以问一个问题来处理这个问题的“条件”的一部分(因为我有告诉你,评论)...

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

用户完成姓氏后,用户名为
+0

好的,我做了你告诉我的,接下来我应该做什么? \t 我不明白的是,这个wocommerce已经有了电话号码和电子邮件。我不需要添加其他字段..可以吗? –

+0

@BursucAndrei我的代码没有创建电子邮件和电话。我只是重新订购现有的电话和电子邮件。现在用我的答案的代码,你可以创建你的新问题,说你有这个代码用于创建/订购结账字段,并且你想要(代码的第二部分)。完成后,请在此发布链接。 – LoicTheAztec