Header Ads Widget

Remove woocommerce payment gateway programmatically

Woocommerce provides payment gateway options under woocommerce settings menu. But at sometimes we want to disable this payment gateway programmatically for debugging purpose or any other reason as per our development logic. So woocommerce provides one filter that shows all list of payment gateways and we can disable or remove it manually by code.

Woocommerce Filter : woocommerce_available_payment_gateways filter provides all active payment gateways list on checkout page. And there you can find gateway name that you want to disable.

function show_active_gateways($gateways){
    global $woocommerce;  
    // unset payment gateway
    return $gateways;

This filter returns one parameter, which contains all gateways name list. Basically its an array so we just have to unset that gateway by its name/array key. So it will remove that key and values from gateway list and return modified array to filter. Just like "paypal", you can see other payment method names as well. ex. cheque, cod, basc etc. if its enabled from admin. You can see this list if you print $gateways variable by using print_r() function.

To remove this payment gateway we have used unset() function provided by php it self. This function expects key from array that we want to remove so ultimately its removing key and value both from array.

By this way, we can remove payment gateway programmatically using woocommerce filter. Thanks.

Post a comment