在 woocommerce 中,当订单总额为 0 美元时显示支票付款选项

In woocommerce show the check payment option when an order total is 0 dollars

在我的 wordpress、woocommerce 网站上,我正在尝试向我的主题 function.php 添加一些内容,如果订单总额为 0 美元,将启用支票支付选项。

我目前拥有的:

function payment_gateway_enable_check( $available_gateways ) {
global $woocommerce;
if ( !isset( $available_gateways['check'] ) && $woocommerce->cart->total == 0 ) {
    set(  $available_gateways['check'] );
}
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_enable_check' );

显然设置不正确,但我不确定用什么代替它。

由于总订单为 0,因此无需付款,因此 woocommerce woocommerce_cart_needs_payment 当总订单为 0 时会给出 false,因此不需要付款方式。

仅供参考 cheque 是网关的名称不是 check

//Allow accepting payments if total is 0 (free order)
add_filter( 'woocommerce_cart_needs_payment', '__return_true' );

//Unset payment gateways we dont want if total is 0 (free order)
function payment_gateway_enable_check( $available_gateways ) {
    global $woocommerce;
    if ( $woocommerce->cart->total == 0 ) {
        unset($available_gateways['cod']);
        unset($available_gateways['bacs']);
    }
        return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_enable_check' );