我通过我的子主题的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]信用卡
名称[__活跃输入区域__]
卡编号[__有效输入区__] 等
[_]采购订单编号
输入数字[__无效输入区域__]