根据 Woocommerce 中选定的运输方式显示隐藏付款方式 [英] Show hide payment methods based on selected shipping method in Woocommerce

查看:31
本文介绍了根据 Woocommerce 中选定的运输方式显示隐藏付款方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在灵活的发货插件表单 wpdesk 中选择指定的发货方式"时,我想隐藏某些付款方式并启用另一种付款方式.

I would like to hide some payment method and enable another one when I select a specified "Shipping Method" in flexible Shipping plugin form wpdesk.

我已经尝试过该代码:

add_filter( 'woocommerce_available_payment_gateways', 'gateway_disable_shipping_326' );
function gateway_disable_shipping_326( $available_gateways ) {
    global $woocommerce;

    if ( !is_admin() ) {
        $chosen_methods  = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = $chosen_methods[0];

        if ( isset( $available_gateways['payment_method_cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate:6' ) ) {
            unset( $available_gateways['payment_method_cod'] );
        }
    }
    return $available_gateways; 
}

还有这个

function my_custom_available_payment_gateways( $gateways ) {
    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
    if ( in_array( 'flat_rate:6', $chosen_shipping_rates ) ) :
        unset( $gateways['payment_method_cod'] );
        endif;
    if ( in_array( 'flat_rate:8', $chosen_shipping_rates ) ) :
        unset( $gateways['payment_method_przelewy24'] );
    endif;
    return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );

我的网站链接:[www.dajati.pl][1]

The link to my website: [www.dajati.pl][1]

推荐答案

以下代码示例将根据选择的送货方式启用/禁用支付网关.

The following code example will enable / disable payment gateways based on chosen shipping method.

在此示例中,我们有 3 种运输方式和 3 个支付网关.每种选定的运输方式将仅启用一个不同的支付网关.

In this example, we have 3 shipping methods and 3 payment gateways. Each selected shipping method will enable only one different payment gateway.

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );
function payment_gateways_based_on_chosen_shipping_method( $available_gateways ) {
    // Not in backend (admin) and Not in order pay page
    if( is_admin() ||  is_wc_endpoint_url('order-pay') ) 
        return $available_gateways;
     
    // Get chosen shipping methods
    $chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );

    if ( in_array( 'flat_rate:12', $chosen_shipping_methods ) )
    {
        unset( $gateways['bacs'] );
        unset( $gateways['cod'] );
    }
    elseif ( in_array( 'flat_rate:14', $chosen_shipping_methods ) )
    {
        unset( $gateways['bacs'] );
        unset( $gateways['paypal'] );
    }
    elseif ( in_array( 'free_shipping:10', $chosen_shipping_methods ) )
    {
        unset( $gateways['cod'] );
        unset( $gateways['paypal'] );
    }

    return $gateways;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

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

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

这篇关于根据 Woocommerce 中选定的运输方式显示隐藏付款方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆