本文介绍了如果在 Woocommerce 中应用了任何优惠券代码,则删除一些支付网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始从事小型 Woocommerce 项目.我有 3 个进入这家商店的支付网关:Paypal、信用卡和直接银行转账.

I started to work on small Woocommerce project. I have 3 payment gateways into this store: Paypal, Credit Card and Direct bank Transfer.

我想要的是:如果使用优惠券代码,我想从可用的支付网关中禁用(或删除)Paypal 和信用卡,并保留直接银行转账"作为可用的支付网关选择.

What I would like is: If coupon code is used, I would like to disable (or remove) Paypal and Credit Card from available payment gateways, and just keep "Direct bank Transfer" as available payment gateway choice.

从结帐页面显示当前状态:

To show how is look current state from checkout page:

我找到了一个类似的解决方案,但这是根据产品类别删除网关.

I found a similar solution, but this is for removing gateway based on product category.

add_filter( 'woocommerce_available_payment_gateways', 'unset_payment_gateways_by_category' );

function unset_payment_gateways_by_category( $available_gateways ) {
    global $woocommerce;

    $unset = false;
    $category_ids = array( 8, 37 );

    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( in_array( $term->term_id, $category_ids ) ) {
                $unset = true;
                break;
            }
        }
    }
    if ( $unset == true ) 
        unset( $available_gateways['cheque'] );

    return $available_gateways;
}

所以我认为可以使用此功能,但根据我的问题稍作修改.

So I think that this function can be used, but slightly modified per my problem.

感谢任何帮助.

推荐答案

以下代码将删除所有支付网关除了直接银行转账" (bacs) 仅当客户至少应用了一个优惠券代码:

The following code will remove all payment gateways except "Direct bank Transfer" (bacs) only if at least one coupon code has been applied by the customer:

add_filter('woocommerce_available_payment_gateways', 'applied_coupons_hide_payment_gateways', 20, 1 );
function applied_coupons_hide_payment_gateways( $available_gateways){
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    // If at least a coupon is applied
    if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
        // Loop through payment gateways
        foreach ( $available_gateways as $gateway_id => $gateway ) {
            // Remove all payment gateways except BACS (Bank Wire)
            if( $gateway_id != 'bacs' )
                unset($available_gateways[$gateway_id]);
        }
    }

    return $available_gateways;
}

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

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

这篇关于如果在 Woocommerce 中应用了任何优惠券代码,则删除一些支付网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:15