简体   繁体   English

如果在 Woocommerce 中应用了任何优惠券代码,则删除一些支付网关

[英]Remove some payment gateways if any coupon code is applied in Woocommerce

I started to work on small Woocommerce project.我开始从事小型 Woocommerce 项目。 I have 3 payment gateways into this store: Paypal, Credit Card and Direct bank Transfer.我有 3 个进入这家商店的支付网关: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.我想要的是:如果使用优惠券代码,我想从可用的支付网关中禁用(或删除)Paypal 和信用卡,并保留“直接银行转账”作为可用的支付网关选择。

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.所以我认为可以使用此功能,但根据我的问题稍作修改。

Any help is appreciated.任何帮助表示赞赏。

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:仅当客户至少应用了一个优惠券代码时,以下代码才会删除除“直接银行转账” (bacs)之外的所有支付网关:

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

Code goes in function.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

here you go :干得好 :

add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');

function unset_gatway_by_applied_coupons($available_gateways)
{

    $coupons = WC()->cart->applied_coupons;

    if (!empty($coupons)) {
        unset($available_gateways['bacs']);
    }

    return $available_gateways;
}

what we did here we checked if any coupons is applied trough WC()->cart->applied_coupons;我们在这里所做的是检查是否有任何优惠券通过WC()->cart->applied_coupons; which will return an array of coupons if coupons array not empty remove specific payment gateway如果优惠券数组不为空,它将返回一个优惠券数组,删除特定的支付网关

if you want to check if certain coupon is applied and remove gatway based on your condition you can use the following:如果您想检查是否应用了某些优惠券并根据您的情况删除网关,您可以使用以下方法:

add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');

function unset_gatway_by_applied_coupons($available_gateways)
{

    $coupons = WC()->cart->applied_coupons;

    foreach ($coupons as $coupon) {

        if ($coupon == 'my_coupon') { //here you can specific your coupon name
            unset($available_gateways['bacs']);
        }

    }

    return $available_gateways;
}

of course both functions are tested, you just need to place them in your functions.php当然这两个函数都经过测试,你只需要把它们放在你的functions.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在 Woocommerce 中应用了任何优惠券,则删除特定状态 - Remove specific states if any coupon is applied in Woocommerce 如果使用优惠券,则删除一个支付网关 - Remove one payment gateway if a coupon is applied 删除已定义产品类别组的 WooCommerce 支付网关 - Remove WooCommerce Payment Gateways for defined groups of product categories 根据 Woocommerce 中的购物车总金额添加或删除支付网关 - add or remove payment gateways based on cart total amount in Woocommerce 在 Woocommerce 中禁用特定优惠券代码的“购物车需要付款” - Disable "cart needs payment" for a specific coupon code in Woocommerce 在管理新订单电子邮件模板中添加应用的优惠券代码 - WooCommerce - Add Applied Coupon Code in Admin New Order Email Template - WooCommerce 应用优惠券代码后更改WooCommerce购物车价格 - Changing WooCommerce cart price after applied coupon code 在WooCommerce中应用特定优惠券代码时发送电子邮件通知 - Send an email notification when a specific coupon code is applied in WooCommerce 当在 WooCommerce 中应用特定优惠券代码时,向客户发送 email 通知 - Send an email notification to customer when a specific coupon code is applied in WooCommerce 替换“优惠券代码申请成功”。 WooCommerce 中的消息 - Replace “Coupon code applied successfully.” Message in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM