根据 Woocommerce 中选择的付款方式禁用运输方式

Posted

技术标签:

【中文标题】根据 Woocommerce 中选择的付款方式禁用运输方式【英文标题】:Disable shipping method based on chosen payment method in 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 );

接下来我应该尝试什么?

【问题讨论】:

【参考方案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 上,但是在第一次执行后,更新进入一个循环并且永远不会停止......有问题吗?

以上是关于根据 Woocommerce 中选择的付款方式禁用运输方式的主要内容,如果未能解决你的问题,请参考以下文章

当用户选择贝宝付款方式时,在结帐表单中禁用 WooCommerce 中的帐单地址

在 WooCommerce 中根据产品类型隐藏付款方式

根据 Woocommerce 选择的付款方式更改结帐时的付款按钮

根据 Woocommerce 选择的付款方式,在结帐时更改带有图像的付款按钮

在 WooCommerce 中禁用基于自定义运输方式的付款方式

在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式