【问题标题】:Custom checkbox show or hide custom notice in Woocommerce checkout自定义复选框在 Woocommerce 结帐中显示或隐藏自定义通知
【发布时间】:2019-08-16 09:44:45
【问题描述】:

我是这个编码方面的新手,我现在研究了许多网站,并尝试在 Woocommerce 中建立自己的结帐字段。它应该是一个结帐字段,当它被选中时,应该会弹出一些信息或警告,它可以正常显示在结帐页面上,但我的脚本不起作用。

add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {
    $fields['billing']['checkbox_trigger'] = array(
        'type'      => 'checkbox',
        'label'     => __('You dont live in Germany?', 'woocommerce'),
        'class'     => array('form-row-wide'),
        'clear'     => true
    );
    return $fields;
}

add_action( 'woocommerce_after_checkout_billing_form', 'echo_notice_billing' );
function echo_notice_billing() {
    echo '<div class="billing-notice woocommerce-info" style="display:none">It may take forever</div>';
}

add_action( 'woocommerce_after_checkout_form', 'show_notice_billing' );
function show_notice_billing(){ 
    ?>
        <script>
        jQuery(document).ready(function($){
            $('checkbox_trigger').change(function(){
                if(this.checked){
                    $('billing-notice').show();
                }
                else {
                    $('billing-notice').hide();
                }
            });
        });
    </script>
    <?php
}

【问题讨论】:

    标签: php jquery wordpress woocommerce checkout


    【解决方案1】:

    您的 jQuery 脚本中有一些错误...尝试用此替换您的相关函数:

    add_action( 'woocommerce_after_checkout_form', 'show_notice_billing' );
    function show_notice_billing(){
        ?>
            <script>
            jQuery(function($){
                $('#checkbox_trigger').click(function(){
                    if($(this).is(':checked') ){
                        $('.billing-notice').show();
                    }
                    else {
                        $('.billing-notice').hide();
                    }
                });
            });
        </script>
        <?php
    }
    

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2021-07-29
      • 2018-10-04
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多