ds3464 2018-05-18 13:25
浏览 85
已采纳

根据Woocommerce中选择的付款方式停用送货方式

I need to disable specific shipping method if user selected payment "Cash on Delivery". The problem is that the following code works only if I reset WooCommerce transients each time and refresh. It doesn't work on user selection back and forth.

add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
function alter_shipping_methods( $rates ) {

    $chosen_gateway = WC()->session->chosen_payment_method;

    // If payment is Cash on delivery remove specific shipping 
    if($chosen_gateway == 'cod') {

        foreach ( $rates as $rate_id => $rate ) {
           if ( $rate->label === 'Hrvatska pošta' ) {
              unset( $rates[ $rate_id ] );
            }
       }

    }

    return $rates;

}

I do have this code which should trigger and I see the output in console when I click around options.

jQuery(document.body).on('change', 'input[name="payment_method"]', function() {
    console.log('Payment method changed');
    jQuery('body').trigger('update_checkout');
});

I have tried with this, it doesn't work

function action_woocommerce_checkout_update_order_review($array, $int) {
    WC()->cart->calculate_shipping();
    return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);

And I have also tried custom AJAX call which calls a PHP function and inside this filter, no result

add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );

What should I try next?

  • 写回答

1条回答 默认 最新

  • douzhaobo6488 2018-05-19 13:20
    关注

    Updated on March 2019

    Here is the complete working way to make "COD" payment method disable a specific shipping method.

    You will have to set in the first function the shipping method Id that you wish to hide.

    The code:

    add_action( 'woocommerce_package_rates','show_hide_shipping_methods', 10, 2 );
    function show_hide_shipping_methods( $rates, $package ) {
        // HERE Define your targeted shipping method ID
        $payment_method        = 'cod';
    
        $chosen_payment_method = WC()->session->get('chosen_payment_method');
    
        if( $payment_method == $chosen_payment_method ){
            unset($rates['flat_rate:12']);
        }
        return $rates;
    }
    
    add_action( 'woocommerce_review_order_before_payment', 'payment_methods_trigger_update_checkout' );
    function payment_methods_trigger_update_checkout(){
        // jQuery code
        ?>
        <script type="text/javascript">
            (function($){
                $( 'form.checkout' ).on( 'blur', 'input[name^="payment_method"]', function() {
                    setTimeout(function(){
                        $(document.body).trigger('update_checkout');
                    }, 250 );
                });
            })(jQuery);
        </script>
        <?php
    }
    
    add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods' );
    function refresh_shipping_methods( $post_data ){
        // HERE Define your targeted shipping method ID
        $payment_method = 'cod';
        $bool           = true;
    
        if ( WC()->session->get('chosen_payment_method') === $payment_method )
            $bool = false;
    
        // Mandatory to make it work with shipping methods
        foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
            WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
        }
        WC()->cart->calculate_shipping();
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    To be able to get the correct shipping method ID you can use your browser inspector, this way:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包