【问题标题】:Add a checkbox to show / hide checkout fields in Woocommerce添加一个复选框以在 Woocommerce 中显示/隐藏结帐字段
【发布时间】:2019-02-18 19:23:17
【问题描述】:

让我们浏览一下场景:

客户在结账页面,有一个带有文字的复选框:“这是礼物吗?”

如果选中复选框,下面的字段将淡入以记录笔记。

基于这个线程和其他一些线程是我的代码:

add_filter( 'woocommerce_checkout_fields' , 'display_checkbox_and_new_checkout_field' );   
function display_checkbox_and_new_checkout_field( $fields ) {

    $fields['billing']['checkbox_trigger'] = array(
        'type'      => 'checkbox',
        'label'     => __('Checkbox label', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true
    );  

    $fields['billing']['new_billing_field'] = array(
        'label'     => __('New Billing Field Label', 'woocommerce'),
        'placeholder'   => _x('New Billing Field Placeholder', 'placeholder', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    return $fields;
}

add_action( 'woocommerce_after_checkout_form', 'conditionally_hide_show_new_field', 6 );
function conditionally_hide_show_new_field() {
      ?>
        <script type="text/javascript">
            jQuery('input#checkbox_trigger').change(function(){

                if (this.checked) {
                    jQuery('#new_billing_field_field').fadeOut();
                    jQuery('#new_billing_field_field input').val('');           
                } else {
                    jQuery('#new_billing_field_field').fadeIn();
                }

            });
        </script>
    <?php
}

问题是选中框时应该淡入的字段已经可见。

如何更改脚本以使其在加载时不可见?

【问题讨论】:

  • 您可以在脚本标签中添加类似的内容以在页面加载时隐藏元素:$(document).ready(function() { $("#new_billing_field").hide(); });
  • @tshimkus 我应该在哪里添加它?该脚本应该专门在结帐页面上加载,但我应该使用哪个文件?
  • 它可以去任何地方,但是代码中现有的脚本标签可以工作。它会在页面加载时隐藏表单。

标签: php jquery wordpress woocommerce checkout


【解决方案1】:

请尝试以下方法:

add_action( 'woocommerce_before_checkout_form', 'conditionally_show_hide_checkout_field' );
function conditionally_show_hide_checkout_field() {
    ?>
    <style>
    p#new_billing_field_field.on { display:none !important; }
    </style>

    <script type="text/javascript">
    jQuery(function($){
        var a = 'input#checkbox_trigger',   b = '#new_billing_field_field'

        $(a).change(function(){
            if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
                $(b).show(function(){
                    $(b).css({'display':'none'}).removeClass('on').show();
                });
            }
            else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
                $(b).fadeOut(function(){
                    $(b).addClass('on')
                });
                $(b+' input').val('');
            }
        });
    });
    </script>
    <?php
}

add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {

    $fields['billing']['checkbox_trigger'] = array(
        'type'      => 'checkbox',
        'label'     => __('Checkbox label', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    $fields['billing']['new_billing_field'] = array(
        'label'     => __('New Billing Field Label', 'woocommerce'),
        'placeholder'   => _x('New Billing Field Placeholder', 'placeholder', 'woocommerce'),
        'class'     => array('form-row-wide on'),
        'clear'     => true
    );

    return $fields;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

  • 非常感谢,从技术上讲,它就像一个魅力。样式只有一个问题。如果您查看link,您会看到复选框和标签彼此相邻。在那里,我在几页上找到了一些 sn-ps,但没有成功。有什么想法吗?
  • 由于您将我的最新问题标记为过于宽泛,因此我使用了revise。我不知道我的编辑是否会弹出,所以我决定从这里打扰你。我认为在这里使用需要一个学习曲线。还是谢谢。
  • @MehmetAliGörür 社区已将您的问题标记为过于宽泛,因为您需要在回答中提供一些代码。请记住,stackOverFlow 不是免费的编码服务。人们只会帮助您编写相关代码,以显示您在解决问题方面的编码工作。
  • 感谢您的善意提醒。下次我会记住这一点。干杯。
  • @Kees 对于多个字段,您可以拥有一个唯一的字符串,其中所有单独的字段选择器以逗号分隔,例如:b = '#new_billing_field_field1,#new_billing_field_field2';... 所以无需使用数组使事情复杂化。另外,如果您喜欢/想要并且此答案有用,请不要忘记投票。
猜你喜欢
  • 1970-01-01
  • 2017-07-25
  • 2019-01-21
  • 1970-01-01
  • 2019-12-28
  • 2017-01-13
  • 2021-07-21
  • 2021-05-14
  • 2020-12-11
相关资源
最近更新 更多