Del
Del

Reputation: 1

Show/Hide payment methods for various product categories in WooCommerce

So, I've seen example of how to unset payment gateways based on product category, such as this one (which works nicely):

add_filter( 'woocommerce_available_payment_gateways', 
'unset_gateway_by_category' );

function unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 11810 );
    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( in_array( $term->term_id, $category_ids ) ) {
                $unset = true;
                break;
            }
        }
    }
    if ( $unset == true ) unset( $available_gateways['worldpay'] );
    if ( $unset == true ) unset( $available_gateways['ppec_paypal'] );
    return $available_gateways;
    }

In this instance, removing payment methods Worldpay and PayPal if any product with product category ID 11810 is in the cart.

But, it doesn't quite cover what I need.

So, if you have categories 1, 2, 3 and 4... along with payment methods a, b, and c.

Product category 4 can only use payment gateway c, and product categories 1,2 and 3 can only use payment gateways a and b.

If there are products from all categories in the cart (including category 4), then the above code would hide payment methods a and b, only leaving gateway c (works perfectly). But... how to unset gateway c, if the cart doesn't contain any products from category 4?

I think I need an elsif in there somewhere, but... I'm no php ninja, so would appreciate a little guidance.

Thanks in advance.

UPDATE:

The version below is the closest so far.

It hides the specialist gateway when there's no restricted products in the cart. It hides the regular gateways when there's only restricted products in the cart.

However... if there's a mic of regular products AND restricted products... it hides all gateways.

add_filter( 'woocommerce_available_payment_gateways', 
'unset_gateway_by_category' );

function unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_ids = array( 15, 166, 11810 );

/*This will dynamically map the payment gateway that need to be hidden for 
the specific category id*/
$category_payment_map = array(
    '15' =>  array('totalprocessing'),
    '166' =>  array('totalprocessing'),
    '11810' =>  array('worldpay', 'ppec_paypal')

    );

foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( in_array( $term->term_id, $category_ids ) ) {
                /*Based on Term Id, fetch the payment gateway that need to 
be hide from the $category_payment_map array*/
                $payment_gateway_ids =  $category_payment_map[$term->term_id];

                /*you can check here whether the $payment_gateway_id is an array or sin*/
                foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
                    unset( $available_gateways[$payment_gateway_id] );
                }

            }
        }
    }
    return $available_gateways;
    }

I assume it needs some sort of rule that says, if cart has a mix of regular products and restricted products, then the restricted products rule applies and overrides the other rules?

Thanks

Upvotes: 0

Views: 1482

Answers (1)

Momin IqbalAhmed
Momin IqbalAhmed

Reputation: 970

Yes, there is some wrong logical reperesentation in your code to unset the payment gateway.I have modified the code and provided the comment on modified code.

Please apply the code and let me know if it is working or not. Hope your issue will be resolved for sure

    add_filter( 'woocommerce_available_payment_gateways', 'unset_gateway_by_category' );

    function unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_ids = array( 11810, 11900 );

    /*This will dynamically map the payment gateway that need to be hide for the specific category id*/
    $category_payment_map = array(
                                   '11810' =>  array('worldpay', 'ppec_paypal'),
                                   '11900' =>  array('ppec_paypal')
                                );

    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );    
        foreach ( $terms as $term ) {        
            if ( in_array( $term->term_id, $category_ids ) ) {
                /*Based on Term Id, fetch the payment gateway that need to be hide from the $category_payment_map array*/
                $payment_gateway_ids =  $category_payment_map[$term->term_id];

                /*you can check here whether the $payment_gateway_id is an array or sin*/
                foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
                    unset( $available_gateways[$payment_gateway_id] );
                }

            }
        }
    }
    return $available_gateways;
    }

Upvotes: 1

Related Questions