SearchWP WooCommerce Integration - Documentation

Overview

The SearchWP WooCommerce Integration plugin (v1.4.0) extends SearchWP to provide enhanced search functionality for WooCommerce product searches. It seamlessly integrates SearchWP’s powerful search capabilities with WooCommerce’s native search, layered navigation, and product filtering systems.

How It Extends SearchWP

Core Integration Mechanism

The plugin extends SearchWP through several key mechanisms:

  1. Query Interception: Intercepts WooCommerce product searches and routes them through SearchWP’s search engine instead of WordPress’s default search

  2. Result Injection: Injects SearchWP results into WooCommerce’s query system while maintaining compatibility with WooCommerce’s layered navigation and filtering

  3. Mod System Integration: Uses SearchWP’s Mod system to customize queries with WooCommerce-specific requirements (sorting, visibility, stock status)

Integration Points

1. Search Query Handling

  • Hook: woocommerce_product_query - Modifies WooCommerce product queries

  • Hook: loop_shop_post_in - Injects SearchWP results into WooCommerce’s post loop

  • Hook: searchwp\native\short_circuit - Controls when SearchWP should handle searches

  • Hook: searchwp\query\mods - Applies WooCommerce-specific modifications to SearchWP queries

2. Admin Integration

  • Hook: searchwp\native\force - Forces SearchWP to handle admin searches when products are involved

  • Supports WooCommerce’s admin product search functionality

3. JSON Search Integration

  • Hook: woocommerce_json_search_found_products - Optionally hijacks WooCommerce’s AJAX product search (admin dropdowns, etc.)

Additional Features

1. WooCommerce Search Integration

Product Search Enhancement

  • Replaces WordPress’s default product search with SearchWP’s advanced search capabilities

  • Maintains compatibility with WooCommerce’s search URL structure (?s=query&post_type=product)

  • Supports both frontend shop searches and admin product searches

Key Methods:

  • is_woocommerce_search() - Detects when a WooCommerce product search is occurring

  • post_in() - Injects SearchWP results into WooCommerce’s query loop

  • product_query() - Modifies WooCommerce product queries to use SearchWP results

2. Layered Navigation Integration

Filtered Term Counts

  • Feature: Ensures WooCommerce Layered Navigation widgets show correct product counts based on SearchWP results

  • Method: get_filtered_term_product_counts_query() - Modifies term count queries to include only SearchWP results

Price Filtering

  • Feature: Limits WooCommerce price filter to only products found by SearchWP

  • Method: price_filter_sql() - Modifies price filter SQL to include SearchWP result IDs

3. WooCommerce Sorting Support

The plugin fully supports all WooCommerce sorting options while maintaining SearchWP relevance:

Supported Sort Options:

  • Relevance (default) - Uses SearchWP’s relevance scoring

  • Price (low to high) - Sorts by product price meta

  • Price (high to low) - Sorts by product price meta (descending)

  • Popularity - Sorts by total sales meta

  • Rating - Sorts by average product rating

  • Date - Sorts by post date

  • Name - Sorts alphabetically by product title

Key Methods:

  • get_woocommerce_ordering() - Retrieves WooCommerce ordering preferences

  • query_orderby() - Builds ORDER BY clauses for SearchWP queries

  • build_orderby_array() - Constructs sorting arrays for different sort types

  • query_main_join() - Adds necessary JOINs for sorting (price, popularity, ratings)

Rating Sorting Implementation

  • Calculates average product ratings from WooCommerce review comments

  • Joins wp_comments and wp_commentmeta tables to aggregate ratings

  • Sorts by average rating, then by post date as a secondary sort

4. Product Visibility Management

Hidden Products Exclusion

  • Feature: Automatically excludes products marked as “exclude-from-search” from SearchWP results

  • Method: exclude_hidden_products() - Identifies and excludes hidden products

  • Filter: searchwp_woocommerce_consider_visibility - Allows disabling visibility checks

Out of Stock Products

  • Feature: Respects WooCommerce setting to hide out-of-stock items from search

  • Method: maybe_exclude_out_of_stock_products() - Excludes out-of-stock products when configured

  • Setting: Respects woocommerce_hide_out_of_stock_items WooCommerce option

5. Meta Query Customization

Visibility Meta Query

  • Feature: Removes WooCommerce’s default visibility meta query when SearchWP is handling searches

  • Method: woocommerce_product_query_meta_query() - Customizes meta queries

  • Rationale: SearchWP handles visibility through its own mechanisms, avoiding conflicts

6. Comments/Reviews Label Customization

UI Enhancement

  • Feature: Changes the label “Comments” to “Reviews” in SearchWP engine configuration for products

  • Method: comments_label() - Customizes attribute labels

  • Filter: searchwp\source\attribute\label - Modifies labels in SearchWP admin

7. Query Variable Management

Search Query Preservation

  • Feature: Preserves original search query while working around WooCommerce’s transient caching

  • Methods:

    • hijack_query_vars() - Temporarily removes search query to prevent WooCommerce caching issues

    • replace_original_search_query() - Restores search query after processing

8. Admin Search Support

  • Feature: Extends SearchWP to handle WooCommerce admin product searches

  • Method: maybe_force_admin_search() - Forces SearchWP to handle admin searches when products are involved

  • Method: set_admin_search_args() - Configures admin search arguments

9. JSON Search Integration (Optional)

  • Feature: Optionally replaces WooCommerce’s AJAX product search with SearchWP results

  • Method: json_search_products() - Hijacks WooCommerce’s JSON search endpoint

  • Filter: searchwp_woocommerce_hijack_json_search - Controls whether to enable this feature

  • Use Case: Admin product dropdowns, order management, etc.

Filter Hooks

The plugin provides numerous filter hooks for customization:

Search Control Filters

  • searchwp_woocommerce_forced - Force SearchWP to handle WooCommerce searches

  • searchwp_woocommerce_short_circuit - Short-circuit SearchWP handling

  • searchwp_woocommerce_log_searches - Control whether searches are logged

Query Modification Filters

  • searchwp_woocommerce_query_args - Modify SearchWP query arguments for WooCommerce searches

  • searchwp_woocommerce_json_search_products_args - Modify arguments for JSON product searches

  • searchwp_woocommerce_force_weight_sort - Control whether to force SearchWP relevance sorting

Visibility Filters

  • searchwp_woocommerce_consider_visibility - Control whether to consider WooCommerce visibility settings

JSON Search Filter

  • searchwp_woocommerce_hijack_json_search - Control whether to hijack WooCommerce JSON searches

Action Hooks

  • searchwp_woocommerce_before_search - Fires before SearchWP WooCommerce search is executed

Technical Implementation Details

SearchWP Mod System Usage

The plugin leverages SearchWP’s Mod system to:

  1. Add custom JOINs for WooCommerce sorting (price, popularity, ratings)

  2. Add custom ORDER BY clauses that respect WooCommerce sorting preferences

  3. Exclude hidden and out-of-stock products from results

  4. Inject average rating calculations for rating-based sorting

Query Flow

  1. Detection: Plugin detects WooCommerce product search via is_woocommerce_search()

  2. Initialization: Sets up hooks for query modification

  3. SearchWP Query: Executes SearchWP query with WooCommerce-specific modifications

  4. Result Injection: Injects SearchWP results into WooCommerce’s loop_shop_post_in filter

  5. Sorting: Applies WooCommerce sorting preferences through SearchWP Mod system

  6. Filtering: Ensures layered navigation and filters work with SearchWP results

Compatibility Considerations

  • Respects WooCommerce’s tax queries and meta queries

  • Maintains compatibility with WooCommerce’s transient caching system

  • Works with WooCommerce’s pagination system

  • Integrates with WooCommerce’s layered navigation widgets

  • Supports WooCommerce’s price filtering

Requirements

  • SearchWP: Core SearchWP plugin must be installed and active

  • WooCommerce: WooCommerce plugin must be installed and active

  • PHP: Version 5.6 or higher

Version Information

  • Current Version: 1.4.0

  • License: GPL v2 or later

  • Author: SearchWP, LLC

Summary

This plugin transforms WooCommerce’s basic search functionality by integrating SearchWP’s advanced search capabilities. It maintains full compatibility with WooCommerce’s native features (layered navigation, filtering, sorting) while providing significantly enhanced search relevance and capabilities. The integration is seamless and transparent to end users, requiring no configuration beyond installing and activating the plugin.