Change Payment Options Based Off Location

WordPress and Woocommerce are a commonly used content management platform for eCommerce. Although it has many useful features, it does not have a way to manipulate payment options dynamically off customer location. The following function was created to alter theses payment options to provide a better user experience and higher conversion rate. I had three tasks I wanted to complete for this project.

1. Local Wholesalers – Show delivery/pick up option.

For local wholesalers I wanted to provide a pick up / delivery option. So if the customer is local and assigned to the wholesaler group, my function will then show both options for picking up the order OR to have the order delivered. Payment for the order will be collected at the time of delivery via credit card or cash.
Wholesaler Checkout

2. Domestic Customers – Pay with payment processor.

All domestic customers should use regular processor since the processing fee is the lowest and also the safest form of payment.
Domestic Customers Checkout

3. International Customers – Pay with Paypal.

AVS and zip code processing can be tricky for international cards. To reduce fraud and lost orders from the processor’s billing requirements international customers should be directed to Paypal where they may pay with debit card or Paypal account.
International Customers Checkout

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// show hide cash on delivery option
add_filter( 'woocommerce_available_payment_gateways' , 'change_payment_gateway', 20, 1);


function change_payment_gateway( $gateways ){
global $woocommerce;
global $wpdb;
$userID = get_current_user_id();
//group
$get_group ='SELECT * from wp_groups_user_group WHERE user_id = '.$userID.' && group_id > 1';
            $group_results = $wpdb->get_results($get_group);
            foreach($group_results as $row) {      
            $group = $row->group_id;
            }  
           
           
//check local
$get_checkbox ='select * from wp_usermeta where meta_key = "c_local" && user_id = '.$userID.'';
        $results = $wpdb->get_results($get_checkbox);
       
        foreach($results as $row) {  
            $local = $row->meta_value;
        }
       
        if($group == 2 && $local == 'y'){
        }else{
            unset($gateways['cod']);
        }
   
       
//check if international
$check_country ='select * from wp_usermeta where meta_key = "billing_country" && user_id = '.$userID.'';
        $results = $wpdb->get_results($check_country);
        foreach($results as $row) {  
            $country = $row->meta_value;
        }
         
         elseif($group < 1 || !$country){
             unset( $gateways['paypal']);

        }else{
            unset( $gateways['nmi'] );
           
        }
   

   
    return $gateways;
}

Developed with PHP and SQL