WooCommerce 中特定付款方式的最大订单金额

问题描述 投票:0回答:1

我们必须在 WooCommerce 中设置支票付款的最大订单金额,并且我们不想使用插件。但我找不到合适的东西,尝试了一些代码,但没有任何效果。

woocommerce payment-method
1个回答
0
投票

根据 在 woocommerce 中隐藏超过一定金额的订单的货到付款答案,您可以使用

'cod'
付款 ID 对其进行更改
'cheque'

对于基于购物车小计的最大订单金额(针对特定付款 ID)

add_filter( 'woocommerce_available_payment_gateways' , 'max_order_amount_for_payment_gateways', 100 );
function max_order_amount_for_payment_gateways( $payment_gateways ){
    if( is_admin() ) {
        return $payment_gateways; // Not in backend (admin)
    }
    
    $targeted_payment_ids = array('cheque'); // Here define the affected payment method IDs
    $threshold_amount  = 100; // Here define the desired subtotal amount threshold 
    $cart_subtotal = WC()->cart->subtotal;

    // Loop through available payment IDs
    foreach ( $payment_gateways as $payment_id => $values ) {
        if( in_array($payment_id, $targeted_payment_ids) && $cart_subtotal >= $threshold_amount ){
            unset($payment_gateways[$payment_id]); // Hide payment method
        }
    }
    return $payment_gateways;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

对于最低订单金额,只需替换

$cart_subtotal >= $threshold_amount


$cart_subtotal < $threshold_amount

© www.soinside.com 2019 - 2024. All rights reserved.