WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Filtering payment gateways (14 posts)

  1. zed28
    Member
    Posted 5 months ago #

    Hi,

    I am trying to filter payment gateways depending on choosen shipping method. I have tried this filter, but it does not work:

    add_filter( 'woocommerce_available_payment_gateways', 'hide_unrelated_gateways');
    
    function hide_unrelated_gateways ($available_gateways) {
    	if( isset( $available_methods['local_delivery']) OR isset( $available_methods['international_delivery'] ) ) {
    		unset($available_gateways['cheque'] );
    
    	}
    	if( isset( $available_methods['local_pickup'])) {
    		unset($available_gateways['cod'] );
    
    	}
        return $available_gateways;
    }

    I am new to WordPress and PHP, so can anybody help me, please?

    Many thanks from Alex

    https://wordpress.org/plugins/woocommerce/

  2. Chirag Vora
    Member
    Posted 5 months ago #

    @Zed28 You can use APG SHIPPING plugin to filter Payment Gateways based on Shipping method. It works with 2.1 onwards only (not with older versions). Give it a try and let me know if you have a problem setting it up.

  3. zed28
    Member
    Posted 5 months ago #

    Hi, Chirag Vora,

    thank you for you reply, but unfortunatelly I am using version 2.0.20.

    Alex

  4. Chirag Vora
    Member
    Posted 5 months ago #

    In that case, you can download and use the old version of the same plugin from github by seeing the changelog. I used it therefore I'm confident that it does work.

  5. Dalibr
    Member
    Posted 5 months ago #

    Hello,

    What I want to do is disable paypal if flat rate option is selected. How can I do that?

    Cheers.

  6. Chirag Vora
    Member
    Posted 5 months ago #

    @Dalidr APG Shipping has as option to enable only a particular payment gateway for a given shipping method. But you will have to setup a flat rate with that plugin instead of WooCommerce flat rate.

    Give it a try in sandbox first and see how it works out for you.

  7. Dalibr
    Member
    Posted 5 months ago #

    @Chirag it didn't work. Are you sure that it works? I enabled the plugin and it doesn't appear in the shipping options at all. Any idea?

  8. Chirag Vora
    Member
    Posted 5 months ago #

    Not sure why that is happening with your setup. It works fine for me.

  9. Dalibr
    Member
    Posted 5 months ago #

    Thanks Chirag.
    Do I have to fill in anything in the settings of AGP?

    As you can see on the images there is not the option AGP.

    http://s27.postimg.org/sjkrlk3z7/image.png
    http://s29.postimg.org/fnvbikt2v/image.png
    http://s30.postimg.org/6x15qsg1t/image.png

    Any idea would be much appreciated!

  10. zed28
    Member
    Posted 5 months ago #

    Hi, Dalibr,

    Plugin did not work for me too (I do not know why...).
    finally, I figured out this problem using simple filter/function:

    add_filter( 'woocommerce_available_payment_gateways', 'hide_unrelated_gateways');

    function hide_unrelated_gateways ($available_gateways) {
    global $woocommerce;
    if( ($woocommerce->session->chosen_shipping_method == 'international_delivery') OR ($woocommerce->session->chosen_shipping_method == 'local_delivery')) {
    unset($available_gateways['cheque'] );

    }
    if( ($woocommerce->session->chosen_shipping_method == 'local_pickup')) {
    unset($available_gateways['cod'] );

    }
    return $available_gateways;
    }

    It simply unsets (hides) unwanted gateways. Replace conditions with your own. Hope it helps.

    Alex

  11. Dalibr
    Member
    Posted 5 months ago #

    Hi Alex,

    Thank you, it looks promising. I copied it into functions.php but it doesn't work, where did you put it?

  12. zed28
    Member
    Posted 5 months ago #

    Place it in you functions.php.

    You need to replace conditions in filter/function with your own.

    If you look at my code, you can see that for my purpose I am hidding "cheque" gateway when user choose "international delivery" or "local delivery" shipping, etc.

    You need to replace my conditions with your own. Then it will work for you.

  13. Chirag Vora
    Member
    Posted 4 months ago #

    APG Shipping 1.4.1 released today fixes the payment gateway bug. Now payment gateways are filtered correctly.

  14. Dalibr
    Member
    Posted 4 months ago #

    Great thank you guys so much!

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.