如果购物车总额超过特定金额,隐藏 WooCommerce BACS 付款

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

当订单金额低于 500 美元时,我试图隐藏银行账户转账付款方式,但代码不起作用。你能帮我找出错误吗?

 add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods_on_checkout' , 1 );

function show_hide_payment_methods_on_checkout( $available_gateways ) {
    // Set minimum cart total
    $minimum_order_total = 500;

    // Get the order total
    $order_total = WC()->cart->get_total( 'edit' );
    echo 'Final Price: ' . $order_total;
    // Check if the order total is less than or equal to the minimum
    if ( $order_total <= $minimum_order_total ) {
        // Check if 'bacs' payment gateway is available and unset it
        if ( isset( $available_gateways['bacs'] ) ) {
            unset( $available_gateways['bacs'] );
        }
    }

    return $available_gateways;
}
php wordpress woocommerce checkout payment-method
1个回答
0
投票

尝试以下方法:

add_filter( 'woocommerce_available_payment_gateways', 'show_hide_bacs_payment_gateway' );
function show_hide_bacs_payment_gateway( $available_gateways ) {
    // Only on checkout page
    if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
        return $available_gateways;

    // Set minimum cart total
    $maximum_total = 500;

    // Check if the order total is less than or equal to the minimum for BACS payment
    if ( WC()->cart->total <= $maximum_total && isset($available_gateways['bacs']) ) {
        unset($available_gateways['bacs']);
    }
    return $available_gateways;
}

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

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