2013-05-06 44 views
3

我通過我的子主題的functions.php將自定義字段添加到WooCommerce的結帳區域。我添加了一些文本輸入,但我需要添加兩個單選按鈕,用於選擇/激活兩個獨立的「集合」文本輸入。在WooCommerce結帳時選擇特定文本輸入的單選按鈕

基本上我需要一種方法讓我的客戶能夠選擇按鈕A:輸入付款信息選擇按鈕B:並輸入採購訂單編號。我需要確保選中的不是的按鈕不會寫入數據庫,並在結帳期間出現錯誤/產生錯誤。

如果您轉到products page並添加產品,請導航到checkout area,您可以看到我已經完成的工作。代碼如下:

add_action('woocommerce_after_order_notes', 'custom_checkout_field'); 
function custom_checkout_field($checkout) { 

echo '<div id="payment_info"><h3>'.__('Payment Information').'</h3>'; 

woocommerce_form_field('credit_card_name', array(
    'type'   => 'text', 
    'class'   => array('form-row-wide'), 
    'label'   => __('Name on Credit Card'), 
    'required' => true, 
    'placeholder'  => __(''), 
    ), $checkout->get_value('credit_card_name')); 

woocommerce_form_field('card_type', array(
    'type'   => 'select', 
    'class'   => array(''), 
    'label'   => __('Card Type'), 
    'required' => true, 
    'placeholder'  => __(''), 
    'options'  => array(
    'American Express' => __('American Express'), 
    'Discover' => __('Discover'), 
    'Mastercard' => __('Mastercard'), 
    'Visa' => __('Visa') 
    )), $checkout->get_value('card_type')); 

woocommerce_form_field('card_number', array(
    'type'   => 'text', 
    'class'   => array(''), 
    'label'   => __('Card Number'), 
    'required' => true, 
    'placeholder'  => __('XXXX-XXXX-XXXX-XXXX'), 
    ), $checkout->get_value('card_number')); 

woocommerce_form_field('card_expiration_month', array(
    'type'   => 'select', 
    'class'   => array('form-row-first'), 
    'label'   => __('Expiration Month'), 
    'required' => true, 
    'placeholder'  => __('- Select Month -'), 
    'options'  => array(
    '1' => __('1'), 
    '2' => __('2'), 
    '3' => __('3'), 
    '4' => __('4'), 
    '5' => __('5'), 
    '6' => __('6'), 
    '7' => __('7'), 
    '8' => __('8'), 
    '9' => __('9'), 
    '10' => __('10'), 
    '11' => __('11'), 
    '12' => __('12') 
    )), $checkout->get_value('card_expiration_month')); 

woocommerce_form_field('card_expiration_year', array(
    'type'   => 'select', 
    'class'   => array('form-row-last'), 
    'label'   => __('Expiration Year'), 
    'required' => true, 
    'placeholder'  => __('- Select Year -'), 
    'options'  => array(
    '2013' => __('2013'), 
    '2014' => __('2014'), 
    '2015' => __('2015'), 
    '2016' => __('2016'), 
    '2017' => __('2017'), 
    '2018' => __('2018'), 
    '2019' => __('2019'), 
    '2020' => __('2020') 
    )), $checkout->get_value('card_expiration_year')); 

woocommerce_form_field('security_code', array(
    'type'   => 'text', 
    'class'   => array('form-row-first'), 
    'label'   => __('Security Code'), 
    'required' => true, 
    'placeholder'  => __(''), 
    ), $checkout->get_value('security_code')); 

woocommerce_form_field('order_number', array(
    'type'   => 'text', 
    'class'   => array('form-row-wide'), 
    'label'   => __('PO Number (if required)'), 
    'placeholder'  => __(''), 
    ), $checkout->get_value('order_number')); 

echo '</div>'; 

所以它需要像這樣:

付款信息

[X]信用卡
名稱[__活躍輸入區域__]
卡編號[__有效輸入區__] 等

[_]採購訂單編號
輸入數字[__無效輸入區域__]

回答

1

在woocommerce中,無法通過這種方式更改您的表單。

您必須將字段添加到形成-billing.php(複印件woocommerce /模板/ chechout /表單billing.php爲{} yourtheme /checkout/form-b​​illing.php)。這個文件包含一個foreach循環,它將你的字段添加到表單中。在此循環中或之後添加您的自定義字段。例如:

<?php foreach ($checkout->checkout_fields['billing'] as $key => $field) : ?> 

    <?php woocommerce_form_field($key, $field, $checkout->get_value($key)); ?> 

<? 
if($key=='billing_first_name') 
{ 
?> 
<input type="radio" name="gender" value="female">Female 
<input type="radio" name="gender" value="male">Male 
<? 
} 
?> 


<?php endforeach; ?> 

您還可以添加在這裏的jQuery(foreach循環後)顯示/隱藏基於用戶的選擇某些字段。例如:

<script type="text/javascript"> 
jQuery('input[name="gender"]').live('change', function(){ alert('gender checked');}); 
</script> 

檢查您的自定義字段(S)輸入提交後。添加到您的functions.php例如:

/** 
* Process the checkout 
**/ 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 

function my_custom_checkout_field_process() { 
    global $woocommerce; 
    if($_POST['gender']=='male') 
     { 
     $woocommerce->add_error('You are a female'); 
     } 
} 

輸入添加到數據庫,例如:

/** 
* Update the order meta with field value 
**/ 
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 

function my_custom_checkout_field_update_order_meta($order_id) { 
    if ($_POST['gender']) update_post_meta($order_id, 'Gender', esc_attr($_POST['gender'])); 
} 

參見:https://gist.github.com/mikejolley/1604009更多示例代碼

相關問題