隐藏 WooCommerce 中特定运输方式的结帐运输字段部分

有人问了下面的问题,我已经回答了。然后他删除它。所以我发布了这个问题和我的回答,因为这对社区很有用:


在 WooCommerce 中,我试图在不重新加载结帐页面的情况下动态更新复选框表单。我有 2 种送货方式:“送货”和“到付”。我试图仅禁用送货地址部分(使用“如果选择的方法是收集,则送往另一个地址”复选框选项)和所有送货字段。


首先我试过这个:


add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 30, 2 );

function carrier_custom_fields( $method, $index ) {

    if( ! is_checkout()) return; // Only on checkout page


    $customer_carrier_method = 'local_pickup:1';



    if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"


    $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];


    // echo $chosen_method_id;


    // If the chosen shipping method is 'legacy_local_pickup' we display

    if($chosen_method_id == $customer_carrier_method ):


        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');


    else :

        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true');



    endif;


}

然后我试了这个:


add_action( 'woocommerce_shipping_method_chosen', 'check_if_local_pickup', 10, 1 );

function check_if_local_pickup( $chosen_method ) {

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

    $chosen_shipping = $chosen_methods[0]; 

    if ($chosen_shipping == 'local_pickup:1') { 

        add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );

        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');

        return true;

    }

    else

    {

        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true');

        return true;


    }

他们两个都不起作用。


如何在 WooCommerce 中隐藏特定选择的运输方式的结帐运输字段部分?


喵喔喔
浏览 128回答 1
1回答

繁花不似锦

PHP 不是这种方式,因为这是“客户端” (而不是“服务器端”)的事件,因此它需要使用 jQuery。因此,要隐藏特定运输方式的运输字段部分,您将使用以下内容:// Auto Show hide checkout shipping fields section based on chosen shipping methodsadd_action( 'wp_footer', 'custom_checkout_field_script' );function custom_checkout_field_script() {&nbsp; &nbsp; // Only on checkout page&nbsp; &nbsp; if( is_checkout() && ! is_wc_endpoint_url() ):&nbsp; &nbsp; // HERE below define your local pickup shipping method&nbsp; &nbsp; $local_pickup = 'local_pickup:1';&nbsp; &nbsp; // Jquery code start&nbsp; &nbsp; ?>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; jQuery(function($){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var a = 'checked',&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = 'input#ship-to-different-address-checkbox',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = 'input[name^="shipping_method"]',&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = '<?php echo $local_pickup; ?>',&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e = c + ':' + a&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f = 'div.woocommerce-shipping-fields,' + b;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 1. On load: when the chosen shipping method is our defined shipping method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $(e).val() === d ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide shipping fields section&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(f).hide();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 2. On shipping method "change" (Live event)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'form.checkout' ).on( 'change', c, function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // When the chosen shipping method is our defined shipping method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $(e).val() === d ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if the checkbox is checked, uncheck it first&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $(b).prop(a) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(b).click();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide shipping fields section&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(f).hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ( $(e).val() !== d ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // show closed shipping fields section with (unchecked checkbox)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(f).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; endif;}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP