微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Woocomerce 自定义字段显示基于单选按钮选择

我有 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','class'           => array('custom-Name-field',$checkout->get_value( 'custom_Name' ) );
        
        woocommerce_form_field( 'custom_Surname','label'           => 'Uzvārds','class'           => array('custom-Surname-field',$checkout->get_value( 'custom_Surname' ) );
    
        woocommerce_form_field( 'custom_PK','label'           => 'Personas Kods','class'           => array('custom-PK-field',$checkout->get_value( 'custom_PK' ) );
    
        woocommerce_form_field( 'custom_registrationnumber','label'           => 'Reģistrācijas numurs','class'           => array('custom-registrationnumber-field',$checkout->get_value( 'custom_registrationnumber' ) );
    
        woocommerce_form_field( 'custom_PVNnumber','label'           => 'PVN maksātāja numurs','class'           => array('custom-PVNnumber-field',$checkout->get_value( 'custom_PVNnumber' ) );
    
        woocommerce_form_field( 'custom_companyname','label'           => 'Uzņēmuma nosaukums','class'           => array('custom-companyname-field',$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 );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。