Add Pick Up Option to Checkout for Local Customers

Custom coded function that will add a checkbox to customer/store profiles for WordPress / Woocommerce customers. When the local box is checked, the specific customer will now have the ability to pick up in store without being charged a shipping rate. This option can be configured to show dynamically. Instead of a checkbox we could show for all customers located in the state of Georgia or even within a certain mileage of an address.

Checkbox in Profile:

Pick Up Option at 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// add custom field to for local stores
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>
<h3> Delivery / Pick Up Available?  </h3>

<?php

//check if option exists
global $woocommerce;
global $wpdb;
$userID = $user->id;

$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;
                }

                ?>
    <table class="form-table">

        <tr>
            <td><label for="c_local">Local Store?</label>
                <input type="checkbox" name="c_local" id="c_local" value="y" class="regular-text" <?php if ($local == 'y'){ echo'checked';}?>/>
               
            </td>
        </tr>

    </table>
<?php }

//save the custom user meta above
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    if (!isset($_POST['c_local'])){
        $_POST['c_local'] = 'n';
    }else{
        $_POST['c_local'] = 'y';}
       
    update_usermeta( $user_id, 'c_local', $_POST['c_local'] );
}

 
// if local customer show/hide above delivery option
add_filter( 'woocommerce_package_rates', 'hide_delivery_shipping', 10, 2 );
 
function hide_delivery_shipping( $rates, $package ) {
global $woocommerce;
global $wpdb;
$userID = get_current_user_id();

$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;
            }  
       
           
$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 ($local === 'y'){
           
            switch($group){
        //pen
                case 3:
                //dropship
                    unset( $rates['free_shipping:1'] );
                    break;
                   
                /*wholesale
                case 2:
                break;*/

            }
        }else{
           
          unset( $rates['free_shipping:1'] );
          unset( $rates['free_shipping:6'] );
        }
}
       
     
return $rates;

}

// hide shipping form if local is selected
add_action( 'woocommerce_after_checkout_form', 'disable_shipping_local_pickup' );
 
function disable_shipping_local_pickup( $available_gateways ) {
global $woocommerce;
 
// hide shipping based on the static choice
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_no_ajax = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping_no_ajax, 'free_shipping:6' ) ) {
 
?>
<script type="text/javascript">
     jQuery('#customer_details .b-customer-ship-details').fadeOut();
    jQuery('#customer_details .b-customer-ship-local').fadeIn();
 
</script>
<?php
     
}

if ( 0 === strpos( $chosen_shipping_no_ajax, 'free_shipping:1' ) ) {
 
?>
<script type="text/javascript">
    jQuery('#customer_details .delivery-note').fadeIn();
 
</script>
<?php
     
}
 
// hide shipping based on the dynamic choice at checkout
?>
<script type="text/javascript">
                jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
    var val = jQuery( this ).val();
    if (val.match("^free_shipping:6")) {
                jQuery('#customer_details .b-customer-ship-details').fadeOut();
                jQuery('#customer_details .b-customer-ship-local').fadeIn();

        } else {
        jQuery('#customer_details .b-customer-ship-details').fadeIn();
        jQuery('#customer_details .b-customer-ship-local').fadeOut();
    }
   
     if (val.match("^free_shipping:1")) {
         jQuery('#customer_details .delivery-note').fadeIn();
     }else{
          jQuery('#customer_details .delivery-note').fadeOut();
     }
         
});
 
</script>
<?php
 
}

Written with PHP, HTML and Jquery. Code should be added to theme functions file so that it is not affected by updates.