Search code examples
phpwordpresswoocommercecart

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


On my wordpress, woocommerce website I am trying to add something to the function.php of my theme that will enable the pay with check option if the order total is 0 dollars.

what I have so far:

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' );

Obviously set isn't correct, but I am not sure what to use in place of it.


Solution

  • Since the total order is 0 there is no need of payment so woocommerce have woocommerce_cart_needs_payment which will give false when total is 0 so no payment methods are required.

    Just fyi cheque is the name of the gateway not 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' );