在 Woocommerce 中显示或隐藏所选运输方式更改的 html 元素

Dic*_*kti 5 php wordpress jquery checkout woocommerce

我正在尝试根据所选的运输方式显示/隐藏结帐页面中的某些元素。我尝试显示/隐藏的页面元素来自另一个插件,因此我尝试更改它们的显示属性。我看过很多线程,例如:

根据 Woocommerce 3 中的运输方式显示或隐藏结帐字段

但它用于结帐字段,我不确定如何为页面元素执行此操作。

然后基于这个答案线程这是我的代码到目前为止:

add_action( 'wp_footer', 'conditionally_hidding_order_delivery_date' );
function conditionally_hidding_order_delivery_date(){
    // Only on checkout page
    if( ! is_checkout() ) return;

    // HERE your shipping methods rate ID "Home delivery"
    $home_delivery = 'distance_rate_shipping';
    ?>
    <script>
        jQuery(function($){
            // Choosen shipping method selectors slug
            var shipMethod = 'input[name^="shipping_method"]',
                shipMethodChecked = shipMethod+':checked';

            // Function that shows or hide imput select fields
            function showHide( actionToDo='show', selector='' ){
                if( actionToDo == 'show' )
                    $(selector).show( 200, function(){
                        $(this).addClass("validate-required");
                    });
                else
                    $(selector).hide( 200, function(){
                        $(this).removeClass("validate-required");
                    });
                $(selector).removeClass("woocommerce-validated");
                $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
            }

            // Initialising: Hide if choosen shipping method is "Home delivery"
            if( $(shipMethodChecked).val() != '<?php echo $home_delivery; ?>' ) {
                showHide('hide','#e_deliverydate_field' );
                showHide('hide','#time_slot_field' );
            }

            // Live event (When shipping method is changed)
            $( 'form.checkout' ).on( 'change', shipMethod, function() {
                if( $(shipMethodChecked).val() == '<?php echo $home_delivery; ?>' ) {
                        showHide('show','#e_deliverydate_field' );
                        showHide('show','#time_slot_field' );
                }
                else {
                        showHide('hide','#e_deliverydate_field' );
                        showHide('hide','#time_slot_field' );
                }
            });
        });
    </script>
    <?php
}
Run Code Online (Sandbox Code Playgroud)

但它没有完全工作(初始化功能不起作用。)

很感谢任何形式的帮助。

补充编辑:

<p class="form-row form-row-wide validate-required" id="e_deliverydate_field" data-priority="" style="display: block;"><label for="e_deliverydate" class="">Date<abbr class="required" title="required">*</abbr></label><span class="woocommerce-input-wrapper"><input class="input-text hasDatepicker" name="e_deliverydate" id="e_deliverydate" placeholder="Choose a Date" value="" style="cursor:text !important;" type="text"></span></p>
Run Code Online (Sandbox Code Playgroud)

我的目标是将 p 元素的显示属性从块更改为无,反之亦然。

Loi*_*tec 6

所需的代码比我的其他答案中使用的代码简单得多,但您需要确保所选择目标运输方式'distance_rate_shipping'我无法测试的。

对于初始化问题,请参阅我的答案末尾公开的解决方案。

简化的所需代码:

// Embedded jQuery script
add_action( 'wp_footer', 'checkout_delivery_date_script' );
function checkout_delivery_date_script() {
    // Only checkout page
    if( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        var a = 'input[name^="shipping_method"]', b = a+':checked',
            c = 'distance_rate_shipping',
            d = '#e_deliverydate_field,#time_slot_field';

        // Utility function that show or hide the delivery date
        function showHideDeliveryDate(){
            if( $(b).val() == c )
                $(d).show();
            else
                $(d).hide('fast');
            console.log('Chosen shipping method: '+$(b).val()); // <== Just for testing (to be removed)
        }

        // 1. On start
        showHideDeliveryDate();

        // 2. On live event "change" of chosen shipping method
        $('form.checkout').on('change', a, function(){
            showHideDeliveryDate();
        });
    });
    </script>
    <?php
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或主题)的 function.php 文件中。它应该工作。


初始化问题

肯定是因为你使用的生成交货日期输出的插件在初始化后延迟了

解决方案可以是在初始化执行时添加一些延迟

所以应该尝试替换这个:

// 1. On start
showHideDeliveryDate();
Run Code Online (Sandbox Code Playgroud)

通过我的代码中的以下内容(将执行延迟调整为高于或低于 500

// 1. On start
setTimeout(function(){
    showHideDeliveryDate();
}, 500);
Run Code Online (Sandbox Code Playgroud)