基于单选按钮选择的WooCommerce结帐显示/隐藏字段

时间:2020-04-08 10:01:31

标签: wordpress woocommerce conditional-statements field checkout

这是我的情况:在结帐页面中,我有两个选项单选按钮:

  • 公司

我需要在结帐页面中添加一些条件字段。

我创建了字段和单选按钮,但是当用户选择一个选项时,我不知道如何创建显示/隐藏字段。

add_action('woocommerce_after_order_notes', 'customise_checkout_field');
function customise_checkout_field($checkout) {
    $chosen = WC()->session->get( 'radio_chosen' );
    $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
    $chosen = empty( $chosen ) ? '0' : $chosen;
    echo '<div id="customise_checkout_field">';
    woocommerce_form_field('custom_question_field', array(
    'type' => 'radio',
    'class' => array(
      'my-field-class form-row-wide'
    ) ,
    'options' => array(
      '0' => 'Person',
      '1' => 'Company',
    ),
    'label' => __('Who you are?') ,
    'required' => true,
    'default' => $chosen,
    ) , $checkout->get_value('custom_question_field'));

    woocommerce_form_field( 'custom_question_text_cod_fisc', array(
      'type'            => 'text',
      'label'           => 'Codice Fiscale',
      'required'        => true,
      'class'           => array('custom-question-cod-fisc-field', 'form-row-wide'),
    ), $checkout->get_value( 'custom_question_text_cod_fisc' ) );

    woocommerce_form_field( 'custom_question_text_p_iva', array(
      'type'            => 'text',
      'label'           => 'P.Iva',
      'required'        => true,
      'class'           => array('custom-question-p-iva-field', 'form-row-wide'),
    ), $checkout->get_value( 'custom_question_text_p_iva' ) );

    woocommerce_form_field( 'custom_question_text_ragione_sociale', array(
      'type'            => 'text',
      'label'           => 'Ragione sociale',
      'required'        => true,
      'class'           => array('custom-question-ragione-sociale-field', 'form-row-wide'),
    ), $checkout->get_value( 'custom_question_text_ragione_sociale' ) );

    echo '</div>';
}

0 个答案:

没有答案