Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/0c2049aa8830130c273d7c1cb8b2f165 to your computer and use it in GitHub Desktop.
Save dwanjuki/0c2049aa8830130c273d7c1cb8b2f165 to your computer and use it in GitHub Desktop.
This recipe will geocode users based on their WooCommerce billing address
<?php
/**
* This recipe will geocode users based on their WooCommerce billing address.
* Run /wp-admin/?pmpromm_process_users=true to run the script.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpromm_batch_process_addresses_override_users(){
if( !empty( $_REQUEST['pmpromm_process_users'] ) ){
mypmpromm_batch_process_addresses_users();
}
}
add_action( 'admin_init', 'mypmpromm_batch_process_addresses_override_users' );
function mypmpromm_batch_process_addresses_users(){
global $wpdb;
$api_key = pmpro_getOption( 'pmpromm_api_key' );
if( !empty( $api_key ) && function_exists( 'pmpromm_geocode_address' ) ){
$users = get_users();
foreach( $users as $result ){
$member_address = array(
'street' => get_user_meta( $result->ID, 'billing_address_1', true ).' '.get_user_meta( $result->ID, 'billing_address_2', true ),
'city' => get_user_meta( $result->ID, 'billing_city', true ),
'state' => get_user_meta( $result->ID, 'billing_state', true ),
'zip' => get_user_meta( $result->ID, 'billing_postcode', true )
);
$coordinates = pmpromm_geocode_address( $member_address );
if( is_array( $coordinates ) ){
update_user_meta( intval( $result->ID ), 'pmpro_lat', $coordinates['lat'] );
update_user_meta( intval( $result->ID ), 'pmpro_lng', $coordinates['lng'] );
}
}
exit('End');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment