• poptartgun

    (@poptartgun)


    WC Version 2.5.5

    I have a specific product that cannot be paid for online for legal reasons, though it can be ordered via COD. I have written a method hook to hide the payment form (Inspire Commerce credit card payments) when that product is in the cart. When I echo out $available_gateways I see two options in the array, ‘cod’ and ‘inspire’ – but when I hide Inspire with the method below, they both go away and I get the error message:

    Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.

    Any thoughts on why I can’t just have COD?

    function dfg_hide_payment_form($available_gateways) {
        if ($_customer['dfg-pay-later-enabled'] == 1) {
            if( is_checkout() ) {  
                global $woocommerce, $_customer;
                $packages = $_customer['dfg-package-ids'];
    
                foreach ($packages as $package) {
                    if (dfg_woo_in_cart($package)) {
                        unset($available_gateways['inspire']);
                    }
                }
            }
        } else {
            unset($available_gateways['cod']);
        }
    }
    
    add_filter('woocommerce_available_payment_gateways', 'dfg_hide_payment_form', 1);
    • This topic was modified 8 years ago by poptartgun.
Viewing 1 replies (of 1 total)
  • rkda

    (@rkda)

    I am trying to do the same thing for a custom user group. Used this code and made some tweaks, but from what I could see you aren’t returning the origin available_gateways array after you unset specific payments.

    Let me know how you go, I was able to get it working with a wholesale user group.

    function dfg_hide_payment_form($available_gateways) {
        if ($_customer['dfg-pay-later-enabled'] == 1) {
            if( is_checkout() ) {  
                global $woocommerce, $_customer;
                $packages = $_customer['dfg-package-ids'];
    
                foreach ($packages as $package) {
                    if (dfg_woo_in_cart($package)) {
                        unset($available_gateways['inspire']);
                    }
                }
            }
        } else {
            unset($available_gateways['cod']);
        }
        
        return $available_gateways;
    }
    
    add_filter('woocommerce_available_payment_gateways', 'dfg_hide_payment_form', 1);
Viewing 1 replies (of 1 total)
  • The topic ‘Conditionally hiding available gateways in WooCommerce’ is closed to new replies.