0

Around April this year, we have to move our business 100% online due to the pandemic. Physical store hardly get one customer per day.

after 5 months, we recognizes pattern; almost all failed COD delivery are customers with no previous purchase and any successful COD for the value below $15 will result in loss.

We have moved all customer with successful purchase to "verified_customer" and due to massive number of account, all test account need to move to "test_customer" to reduce possibility of error but must retain all "customer" function.

I search the internet for the php and tried this

add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
    global $woocommerce, $cart_subtotal, $current_user;

    if ( $cart_subtotal >= 15 ) {        
        $userRole = implode(',',$current_user->roles);
        if($userRole == 'customer'){
            //-- Remove COD if cart >=$15 belongs to a customer
            unset($gateways['cod']);        
        }   
    }else{
        //-- Hide COD if Subtotal is less than 15 
        unset($gateways['cod']);
    }           
 return $gateways;
} 

This function manage to disable COD payment method for "customer" but not "test_customer". Our test acccount did not experience what an actual customer is experiencing for now and also cart value limit of $15 is somehow doesn't work using this.

Is this even possible?

2
  • I don't know how WooCommerce manages roles but if you want to test for a user role you probably want in_array(). Your code here will only match users with the single role customer.
    – Rup
    Oct 15 '20 at 11:39
  • Hey @Rup. Thanks. I've updated if ($userRole == 'customer') to if ( ($userRole == 'customer') or ($userRole == 'test_customer') ) and figured out if ( in_array ( 'customer','test_customer', (array) $user->roles) ) might be useful someday. Is this the right way to use 'in_array()' function? Oct 15 '20 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.