Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ideadude/f887ad929d0c5f1724f1da137d748e5a to your computer and use it in GitHub Desktop.
Save ideadude/f887ad929d0c5f1724f1da137d748e5a to your computer and use it in GitHub Desktop.
Update PMPro member and order searches to also search by discount code.
/**
* Update member and order searches to also search by discount code.
*
* I'm not recommending anyone actually use this, but sharing as an example.
*
* This is a little hacky how the members list SQL is edited.
* You get odd results if you change the level or other parameters
* on the members list table and also search for an order.
* Can also act wonky if you have overlaps between your discount code names
* and cities, user names, names, or anything else that would normally
* score a hit for a search.
*/
function discount_code_search_for_pmpro_members_list( $sqlQuery ) {
global $wpdb;
if(isset($_REQUEST['s']))
$s = sanitize_text_field(trim($_REQUEST['s']));
else
$s = "";
// is this a discount code?
$code = $wpdb->get_row( "SELECT * FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql( $s ) . "' LIMIT 1" );
if ( ! empty( $code ) ) {
// Get user's using this code
$user_ids_uses = $wpdb->get_col( "SELECT DISTINCT(user_id) FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = '" . esc_sql( $code->id ) . "'");
$user_ids_users = $wpdb->get_col( "SELECT DISTINCT(user_id) FROM $wpdb->pmpro_memberships_users WHERE code_id = '" . esc_sql( $code->id ) . "'");
$user_ids = array_unique( array_merge( $user_ids_uses, $user_ids_users ) );
if ( ! empty( $user_ids ) ) {
$sql_to_match = "WHERE mu.membership_id > 0 AND ";
$sql_to_swap = "WHERE mu.membership_id > 0 AND mu.user_id IN(" . implode( ',', $user_ids ) . ") OR ";
$sqlQuery = str_replace( $sql_to_match, $sql_to_swap, $sqlQuery );
}
}
return $sqlQuery;
}
add_filter( 'pmpro_members_list_sql', 'discount_code_search_for_pmpro_members_list' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment