Woocomerce 自定义字段显示基于单选按钮选择

时间:2021-03-02 15:10:21

标签: wordpress woocommerce custom-wordpress-pages woothemes

我有 3 个单选按钮和 6 个字段,需要根据单选按钮的选择来显示。有人可以帮我吗?

 if( !function_exists( 'custom_checkout_question_field' ) ) {
    
      function custom_checkout_question_field( $checkout ) {
    
        echo "<div class='custom-question-field-wrapper custom-question-1'>";
       woocommerce_form_field( 'custom_question_field', array(
          'type'            => 'radio',
          'required'        => true,
          'class'           => array('custom-question-field', 'form-row-wide'),
          'options'         => array(
            'privat'         => 'Privatpersona',
            'juri'    => 'Juridiska',
            'comp'    => 'Uznemums',
          ),
        ), $checkout->get_value( 'custom_question_field' ) );
    
        woocommerce_form_field( 'custom_Name', array(
          'type'            => 'text',
          'label'           => 'Vārds',
          'required'        => true,
          'class'           => array('custom-Name-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_Name' ) );
        
        woocommerce_form_field( 'custom_Surname', array(
          'type'            => 'text',
          'label'           => 'Uzvārds',
          'required'        => true,
          'class'           => array('custom-Surname-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_Surname' ) );
    
        woocommerce_form_field( 'custom_PK', array(
          'type'            => 'text',
          'label'           => 'Personas Kods',
          'required'        => true,
          'class'           => array('custom-PK-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_PK' ) );
    
        woocommerce_form_field( 'custom_registrationnumber', array(
          'type'            => 'text',
          'label'           => 'Reģistrācijas numurs',
          'required'        => true,
          'class'           => array('custom-registrationnumber-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_registrationnumber' ) );
    
        woocommerce_form_field( 'custom_PVNnumber', array(
          'type'            => 'text',
          'label'           => 'PVN maksātāja numurs',
          'required'        => true,
          'class'           => array('custom-PVNnumber-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_PVNnumber' ) );
    
        woocommerce_form_field( 'custom_companyname', array(
          'type'            => 'text',
          'label'           => 'Uzņēmuma nosaukums',
          'required'        => true,
          'class'           => array('custom-companyname-field', 'form-row-wide'),
        ), $checkout->get_value( 'custom_companyname' ) );
    
    
        echo "</div>";
    
      }
    
      add_action( 'woocommerce_after_checkout_billing_form', 'custom_checkout_question_field' );
    }
    
    if( !function_exists( 'custom_question_conditional_javascript' ) ) {
      function custom_question_conditional_javascript() {
        ?>
        <script type="text/javascript">
        (function() {
    
          // Check if jquery exists
          if(!window.jQuery) {
            return;
          };
    
          var $ = window.jQuery;
    
          $(document).ready(function() {
    
            var
                questionField       = $('custom-question-field'),
                NameField                = $('custom-Name-field'),
                SurnameField             = $('custom-Surname-field'),
                PKField                  = $('custom-PK-field'),
                registrationnumberField  = $('custom-registrationnumber-field'),
                PVNnumberField           = $('custom-PVNnumber-field'),
                companynameField         = $('custom-companyname-field');
    
            }
    
            function toggleVisibleFields() {
                var selectedAnswer = questionField.find('input:checked').val();
                if(selectedAnswer === 'privat') {
                NameField.show();
                SurnameField.show();
                PKField.hide();
                registrationnumberField.hide();
                PVNnumberField.hide();
                companynameField.hide();
              } else if (selectedAnswer === 'juri') {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.show();
                PVNnumberField.show();
                companynameField.show();
              } else if(selectedAnswer === 'comp') {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.show();
                PVNnumberField.hide();
                companynameField.show();
              } else {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.hide();
                PVNnumberField.hide();
                companynameField.hide();
              }
        }
              
    
            $(document).on('change', 'input[name=custom_question_field]', 
            toggleVisibleFields;
            $(document).on('updated_checkout', toggleVisibleFields;
            toggleVisibleFields();
    
          });
        })();
        </script>
        <?php
      }
    
      add_action( 'wp_footer', 'custom_question_conditional_javascript', 1000 );

0 个答案:

没有答案