• Resolved tbonge

    (@tbonge)


    I have changed WC_Query::get_main_search_query_sql() to return a specific result when customers search for a specific keyword. But there is no hook for WC_Query::get_main_search_query_sql() so I have to change the original class file, is there any way to change the function without using a hook and without changing the original class file?

    What I did was check to see if the customer searches for the term ‘reorder’ it shows them the items they have recently ordered and for the term ‘bestselling’ it returns the items they have ordered the most. We are a wholesaler and our customers often like to know what items have high turnover for them.

    Here is my modified version of get_main_search_query_sql()

    	/**
    	 * Based on WP_Query::parse_search ** MODIFIED TO ADD CUSTOM SEARCH RESULT SET **
    	 */
    	public static function get_main_search_query_sql() {
    		global $wpdb;
            global $user_ID;
            
    		$args         = self::$product_query->query_vars;
    		$search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array();
    		$sql          = array();
    
    		switch (strtolower($search_terms[0])) {
    			case "reorder":	
    				$customer_id = get_user_meta($user_ID, 'rpr_store_no', true);
    				$reorder_products = implode(",", tb_customer_reorder ($customer_id));
    				$sql[] = "ID IN ({$reorder_products})";
    				break;
    			case "bestselling":	
    				$customer_id = get_user_meta($user_ID, 'rpr_store_no', true);
    				$bestSelling_products = implode(",", tb_customer_bestSelling ($customer_id));
    				$sql[] = "ID IN ({$bestSelling_products})";
    				break;	
    			default:
    
    		foreach ( $search_terms as $term ) {
    			// Terms prefixed with '-' should be excluded.
    			$include = '-' !== substr( $term, 0, 1 );
    
    			if ( $include ) {
    				$like_op  = 'LIKE';
    				$andor_op = 'OR';
    			} else {
    				$like_op  = 'NOT LIKE';
    				$andor_op = 'AND';
    				$term     = substr( $term, 1 );
    			}
    
    			$like  = '%' . $wpdb->esc_like( $term ) . '%';
    			$sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like ); // unprepared SQL ok.
    		}
            }
    		if ( ! empty( $sql ) && ! is_user_logged_in() ) {
    			$sql[] = "($wpdb->posts.post_password = '')";
    		}
    
    		return implode( ' AND ', $sql );
    	}
Viewing 1 replies (of 1 total)
  • con

    (@conschneider)

    Engineer

    Hi there,

    is there any way to change the function without using a hook and without changing the original class file?

    I am afraid not. You would have to either use another hook / method or encapsulate this in your own plugin file that mimics the core function.

    Kind regards,

Viewing 1 replies (of 1 total)

The topic ‘Changing function without a hook (WC_Query::get_main_search_query_sql() )’ is closed to new replies.