简体   繁体   English

禁用基于 WooCommerce 购物车总数的付款方式

[英]Disable payment methods based on WooCommerce cart total

I tried this code below to hide/disable Credit/Debit card and Direct bank transfer payment method on Woo commerce(WordPress) when the checkout total == 400 but did not work.当结帐总额 == 400 时,我尝试使用下面的代码来隐藏/禁用信用卡/借记卡和 Woo commerce(WordPress) 上的直接银行转帐付款方式,但没有成功。 Please any idea on how to achieve this?请对如何实现这一目标有任何想法? Thank you so kindly.非常感谢你。

function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;

    if ( isset( $available_gateways['bacs'] ) && $woocommerce->cart->total == 400 ) {
        unset(  $available_gateways['bacs'] );
    }
    
    if ( isset( $available_gateways['youpay'] ) && $woocommerce->cart->total == 400 ) {
        unset(  $available_gateways['youpay'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );

Why using a fixed total?为什么使用固定总数? There is very few chances that any customer get speifically 400 as total.任何客户总共获得 400 的机会很少。 It should be "up to 400" instead, so something like if( $tolal >= 400 ) .它应该是“最多 400”,所以类似于if( $tolal >= 400 )

Also "Debit/Credit Cards" doesn't seem to be the right Payment method Id… See [this thread][1] to find out the right Payment method Id for "Debit/Credit Cards" payment gateway.此外,“借记卡/信用卡”似乎不是正确的付款方式 ID……请参阅 [此线程][1] 以找出“借记卡/信用卡”支付网关的正确付款方式 ID。

Try the following (assuming that "Debit/Credit Cards" payment method id is correct) :尝试以下操作(假设“借记卡/信用卡”付款方式 ID 正确)

add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods' );
function show_hide_payment_methods( $available_gateways ) {

    if ( WC()->cart->total >= 400 ) {
        if ( isset($available_gateways['bacs']) ) {
            unset($available_gateways['bacs']);
        }
        if ( isset($available_gateways['Debit/Credit Cards']) ) {
            unset($available_gateways['Debit/Credit Cards']);
        }
    }

    return $available_gateways;
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 It should works.它应该有效。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM