Skip to content

Instantly share code, notes, and snippets.

@ramonfincken
Last active May 2, 2024 17:18
Show Gist options
  • Save ramonfincken/8a671dcaa496425b4d37782cb7c21701 to your computer and use it in GitHub Desktop.
Save ramonfincken/8a671dcaa496425b4d37782cb7c21701 to your computer and use it in GitHub Desktop.
Add search by customer IP address to Woocommerce order search
<?php
// NOTE, see updates function managedwphosting_woocommerce_order_query_args
/**
* Inspired by https://stackoverflow.com/questions/53140009/add-search-by-customer-ip-address-to-woocommerce-order-search
* Code idea borrowed from https://www.skyverge.com/blog/filtering-woocommerce-orders/ && https://gist.github.com/bekarice/41bce677437cb8f312ed77e9f226a812
*/
add_filter( 'request', 'filter_orders_by_payment_method_query' );
function filter_orders_by_payment_method_query( $vars ) {
global $typenow;
if ( 'shop_order' === $typenow && isset( $_GET['_shop_order_ip'] ) ) {
$vars['meta_key'] = '_customer_ip_address';
$vars['meta_value'] = wc_clean( $_GET['_shop_order_ip'] );
}
return $vars;
}
// UPDATED: April 11, 2024
// This is for HPOS
/**
* Inspired by https://stackoverflow.com/questions/53140009/add-search-by-customer-ip-address-to-woocommerce-order-search
* Code idea borrowed from https://www.skyverge.com/blog/filtering-woocommerce-orders/ && https://gist.github.com/bekarice/41bce677437cb8f312ed77e9f226a812
*/
add_filter( 'woocommerce_order_query_args', 'managedwphosting_woocommerce_order_query_args' );
function managedwphosting_woocommerce_order_query_args( $args ) {
if ( isset( $_GET['page'] ) && $_GET['page'] === 'wc-orders' && isset( $_GET['_shop_order_ip'] ) ) {
$args['ip_address'] = wc_clean( $_GET['_shop_order_ip'] );
}
return $args;
}
@ramonfincken
Copy link
Author

wp-admin/edit.php?post_status=all&post_type=shop_order&_shop_order_ip=127.0.0.1

@whatsdd
Copy link

whatsdd commented Apr 10, 2024

wp-admin/edit.php?post_status=all&post_type=shop_order&_shop_order_ip=127.0.0.1
@ramonfincken Is it code still valid ? as format of this has changed with HPOS enabled :)

@ramonfincken
Copy link
Author

ramonfincken commented Apr 11, 2024

You are right @whatsdd

I have updated the code for HPOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment