Created
January 16, 2019 15:59
-
-
Save ideadude/f887ad929d0c5f1724f1da137d748e5a to your computer and use it in GitHub Desktop.
Update PMPro member and order searches to also search by discount code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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