I added a "Customer Country" column to the WooCommerce orders table (in admin dashboard), but I can't figure how to make it sortable. Any help? It would be a nice bonus if it could be made also searchable.
add_filter( 'manage_edit-shop_order_columns', function( $columns ) {
$columns['customer_country'] = 'Customer Country';
return $columns;
}, 10, 1 );
add_action( 'manage_shop_order_posts_custom_column', function( $column ) {
global $post;
if( 'customer_country' === $column ) {
$order = wc_get_order( $post->ID );
echo get_user_geo_country( $order->get_customer_ip_address() );
}
}, 10, 1 );
add_filter( 'manage_edit-shop_order_sortable_columns', function( $columns ) {
$columns['customer_country'] = 'customer_country';
return $columns;
}, 10, 1 );
add_action( 'pre_get_posts', function( $query ) {
if( ! is_admin() ) { return; }
// ????
}, 10, 1 );
/** Get geolocated country name by IP **/
function get_user_geo_country( $user_ip ) {
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ]; // return the country name
}
source https://stackoverflow.com/questions/70136921/add-a-sortable-searchable-customer-country-column-to-the-woocommerce-orders-ta
Comments
Post a Comment