仅在结帐时禁用特定运输方式的支付网关 [英] Disable Payment Gateway For Specific Shipping Method On Checkout Only

查看:85
本文介绍了仅在结帐时禁用特定运输方式的支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WooCommerce 中遇到了一个问题,我想知道是否有其他人也遇到过.

I ran into an issue in WooCommerce and I'm wondering if anyone else has experienced it too.

我销售的某些产品太易碎,无法通过 UPS/DHL/FedEx 运输.所以我必须通过托盘运送这些产品.为了解决我的问题,我创建了一个请求报价"运输方式,允许我的客户选择 BACS 作为付款方式,请求报价作为运输方式并提交他们的订单.在计算运费后,我更新订单(将运输方式更改为 N/A)并将状态从暂停"更改为暂停".到待付款"允许客户根据需要使用卡付款.

I sell certain products that are too fragile to be shipped by UPS/DHL/FedEx. So I have to ship these products via pallet. To solve my problem I created a "request a quote" shipping method that allows my customers to select BACS as the payment method, request a quote as the shipping method and submit their orders. And after I have calculated the shipping costs, I update the order (change the shipping method to N/A) and change the status from "on-hold" to "pending payment" to allow the customer to pay by card if they wish to.

这是我遇到问题的地方.我注意到,如果我取消设置多个支付网关并且选择了这些特定的运输方式,则这些支付网关在订单-支付"模式中对客户不可用.端点(网站/我的帐户/订单/),即使我从订单中删除了送货方式.

This is where I ran into the issue. I noticed that if I unset several payment gateways and if these certain shipping methods are selected, these payment gateways are not available to the customer in the "order-pay" endpoint (website/my-account/orders/) even if I remove the shipping method from the order.

有没有办法解决这个问题?

Is there a way around this?

这是我用来禁用特定运输方式的支付网关的代码.

This is the code I am using to Disable Payment Gateway For Specific Shipping Method.

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
    $shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}

更新在咨询了几位开发人员后,他们建议每次需要支付网关时都会运行此代码,并建议我仅在结帐页面上运行此代码段.

UPDATE After consulting with a few developers they advised that this code runs every time Payment Gateways are required and suggested that I run this snippet only on the Checkout page.

他们建议在我的代码中添加以下内容:

They suggested adding the following to my code:

if ( is_checkout_pay_page() ) {
    // unset Payment Gateways
}

已解决 这是我的尝试,并且有效.但不确定我们是否可以更好地表达:

SOLVED Here's my attempt and it works. But not sure if it can we expressed better:

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    if ( ! ( is_checkout_pay_page() ) ) {

    $gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
    $shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}
else { return $available_gateways;
    }
}

推荐答案

add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );

function filter_woocommerce_available_payment_gateways( $available_gateways ) { 

    if ( ! ( is_checkout_pay_page() ) ) {

    $gateways_to_disable = array( 'paymentgateway1', 'paymentgateway2', 'paymentgateway3' );
    $shipping_methods = array( 'shippingmethod1', 'shippingmethod2', 'shippingmethod3' );
    $disable_gateways = false;

    // Check if we need to disable gateways
    foreach ( $shipping_methods as $shipping_method ) {
        if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
    }
    
    // If so, disable the gateways
    if ( $disable_gateways ) {
        foreach ( $available_gateways as $id => $gateway ) {
            if ( in_array( $id, $gateways_to_disable ) ) {
                unset( $available_gateways[$id] );
            }
        }
    }
    return $available_gateways;
}
else { return $available_gateways;
    }
}

这篇关于仅在结帐时禁用特定运输方式的支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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