Daniel Immke

Selectively disabling payment gateways in WooCommerce

I recently wrapped up a big WooCommerce project that required quite a bit of customization. I’ve decided to write about a few of those customizations and share code snippets.

Here’s a Scenario

The companies that process credit card transactions charge a fee to do so. It’s usually a flat amount plus a small percentage of the transaction. It’s why sometimes at locally owned gas stations you will see “$5 minimum card purchase” signs. This is a cost of doing business that is usually built into the amount something costs—but what if you have a product that costs a lot of money and is so low margin that the fees will eat up any profit you’ll make? That might sound like an edge scenario, but it’s actually pretty common for certain types of businesses. The solution is to disable credit cards and force the purchaser to use an ACH transfer gateway.

With WooCommerce it is simple to selectively disable gateways based on the total amount of a cart. Check it out:

<?php
/** 
 * Disable credit card gateway if the total order exceeds $3,000.
 */
function danieldo_filter_gateways($gateways){
    global $woocommerce;

    // Check if the total cart amount is more than $3,000.
    if( $woocommerce->cart->cart_contents_total >= 3000.00 ) {
        // If it is, remove the gateway.
        unset($gateways['gateway_name']);

    }

    return $gateways;
}

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

Here we are checking to see the total amount and if it is above $3000 we are disabling gateway_name. Of course, change gateway_name to the name of your credit card gateway when using this code and the total amount.

Hey — My name is Daniel Immke. I’m a designer & developer currently building Pickwick.

If you liked my writing, check out more posts or subscribe.