【问题标题】:Disable shipping method based on chosen payment method in Woocommerce根据 Woocommerce 中选择的付款方式禁用运输方式
【发布时间】:2018-10-28 22:59:01
【问题描述】:

如果用户选择“货到付款”付款,我需要禁用特定的送货方式。问题是以下代码仅在我重置时才有效 WooCommerce 每次都会瞬变并刷新。它不适用于来回用户选择。

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;

}

我确实有这段代码应该触发,当我点击选项时,我会在控制台中看到输出。

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

这个我试过了,不行

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);

我还尝试了自定义 AJAX 调用,它调用了一个 PHP 函数,在这个过滤器中,没有结果

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

接下来我应该尝试什么?

【问题讨论】:

    标签: php jquery ajax woocommerce shipping


    【解决方案1】:

    2019 年 3 月更新

    对于 COD 支付网关,您只需在其设置中添加您想要为其启用的“统一费率”运输方式,例如:

    对于 Cod 和其他方式或其他支付网关,这是为特定支付网关禁用特定运输方式的完整工作方式。

    您必须在第一个函数中设置您希望隐藏的运输方式 ID。

    代码:

    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( 'change 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();
    }
    

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

    为了能够获取正确的送货方式 ID,您可以使用浏览器检查器,如下所示:

    在测试此代码之前,您可能需要清空购物车。

    【讨论】:

    • 嗨 LoicTheAztec,您能否更新一下以隐藏其他付款方式?
    • @FilipeOS 怎么样(我没听懂)? ......这个答案正在发挥作用。
    • 根据您的回答,如果您选择货到付款运输方式,其他付款方式仍会显示,您可以要求货到付款+支付,例如使用贝宝。这就是为什么我想隐藏货到付款的付款方式已选择送货。
    • @FilipeOS 对不起,代码不再工作了......所以我已经改变了它,我已经删除了不需要的 Ajax。试试看,给我一些反馈。
    • @LoicTheAztec 我安装在最后一个版本的 WP 和 WC 上,但是在第一次执行后,更新进入一个循环并且永远不会停止......有问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2019-02-07
    • 2021-06-07
    • 2018-08-30
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多