A WooCommerce chatbot becomes substantially more useful when it can search the store catalog instead of relying on a manually maintained list of products. Product search allows the chatbot to answer questions such as “Which blue running shoes are under $100?” or “Do you sell a replacement filter for this model?” with current catalog data.
The safest design is to give the chatbot narrowly scoped, read-only access to a product-search capability rather than direct access to the entire WordPress database or unrestricted WooCommerce administration APIs.
Define what the chatbot needs to search
Before creating an integration, decide which product fields the chatbot is allowed to use. A typical search response may include:
- Product ID and parent product ID
- Product name, short description, and description excerpt
- Product URL and product image URL
- Product categories and tags
- Current price and sale price
- Stock status and purchasability
- Product attributes such as size, color, material, or compatibility
- Variation options when the product is variable
Do not automatically expose internal notes, supplier information, cost prices, private metadata, customer data, unpublished products, or restricted product content. The chatbot should receive only the fields required to answer customer questions.
Choose a product-search endpoint
There are two common approaches:
- Use the WooCommerce REST API. This is appropriate when the chatbot service is a trusted server-side application that can securely store API credentials.
- Create a dedicated read-only WordPress REST endpoint. This is often preferable when the chatbot needs a limited response format, custom search behavior, or anonymous access through a controlled integration layer.
WooCommerce REST API keys should be created with read permission only. Never place a WooCommerce consumer secret in browser-side JavaScript, a public chatbot widget, or a prompt. If the chatbot runs in the browser, route requests through your own server and keep credentials on the server.
Creating a controlled WordPress search endpoint
A custom endpoint can translate a plain search term into a bounded WooCommerce product query. The following example registers /wp-json/store-chat/v1/products and returns a deliberately small public data set.
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'store-chat/v1', '/products', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'store_chat_search_products',
'permission_callback' => '__return_true',
'args' => array(
'q' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => function ( $value ) {
return is_string( $value ) && mb_strlen( trim( $value ) ) >= 2;
},
),
'page' => array(
'default' => 1,
'sanitize_callback' => 'absint',
),
),
) );
} );
function store_chat_search_products( WP_REST_Request $request ) {
$term = trim( $request->get_param( 'q' ) );
$page = max( 1, (int) $request->get_param( 'page' ) );
$query = new WC_Product_Query( array(
'status' => 'publish',
'limit' => 8,
'page' => $page,
'search' => $term,
'orderby' => 'relevance',
'return' => 'objects',
) );
$results = array();
foreach ( $query->get_products() as $product ) {
if ( ! $product->is_visible() ) {
continue;
}
$results[] = array(
'id' => $product->get_id(),
'name' => $product->get_name(),
'url' => $product->get_permalink(),
'image' => wp_get_attachment_image_url( $product->get_image_id(), 'medium' ),
'price' => $product->get_price_html(),
'stock' => $product->is_in_stock() ? 'in_stock' : 'out_of_stock',
'purchasable'=> $product->is_purchasable(),
'summary' => wp_strip_all_tags( $product->get_short_description() ),
);
}
return rest_ensure_response( array(
'query' => $term,
'results' => $results,
) );
}
This example is intentionally limited. It searches published products, returns no more than eight results, excludes products that are not visible, and removes HTML from the short description. In a production implementation, add rate limiting, request logging, caching, and an authentication or signature check when the endpoint is not intended for public use.
Improve search beyond the default product query
The default WooCommerce search may not provide the relevance customers expect, particularly for SKU searches, synonyms, attributes, or compatibility terms. Consider supporting several structured filters instead of putting every requirement into one natural-language query.
For example, the chatbot can convert a customer request into a request such as:
GET /wp-json/store-chat/v1/products?q=running+shoes&category=footwear&attribute_pa_color=blue&max_price=100&in_stock=1
The endpoint can then validate each filter and construct a WC_Product_Query or a purpose-built search query. Use an approved list of filter names and values. Do not allow the chatbot to submit arbitrary meta keys, SQL fragments, or unvalidated query arguments.
For larger catalogs, a dedicated search index such as an optimized WooCommerce search extension or an external search service can provide better ranking and faceting. Keep the index synchronized when products, prices, stock, or visibility change. Search results should still be checked against current WooCommerce data before the chatbot claims that an item is available.
Handle variations and product attributes
Variable products require special care. A parent product may be visible while the requested size or color variation is unavailable. If the chatbot answers questions about specific options, return variation-level information or provide a separate availability lookup.
A useful response can distinguish between:
- The parent product, which contains the general description and product URL
- Available attribute values, such as “blue” and “black”
- Specific variation prices and stock status
- Required selections before adding the product to the cart
Do not let the chatbot invent a combination of attributes. It should confirm that the requested combination exists and is purchasable before presenting it as an available option.
Give the chatbot structured results
Natural-language product descriptions are harder for a chatbot to use reliably than consistent fields. A response might look like this:
{
"query": "blue running shoes under 100",
"results": [
{
"id": 1842,
"name": "Trail Runner",
"url": "https://example.com/product/trail-runner/",
"price": "79.00",
"currency": "USD",
"stock_status": "instock",
"attributes": {
"color": ["Blue"],
"size": ["8", "9", "10"]
}
}
]
}
Use numeric price fields alongside formatted display values when the chatbot must compare prices. Include a currency code, a clear stock status, and a product URL. If a value is unknown, return null rather than an ambiguous empty string.
Connect the search tool to the chatbot
When using tool calling, define a narrow function such as search_products. Its arguments might include query, category, min_price, max_price, in_stock_only, and page. The tool description should state that the function searches the current catalog and that results, not the model’s general knowledge, are the authority for product availability.
A typical flow is:
- The customer asks for a product or applies a filter.
- The chatbot extracts the search term and validated filters.
- The server calls the protected product-search endpoint.
- The endpoint returns a bounded set of structured results.
- The chatbot presents relevant products with accurate names, prices, availability, and links.
- The chatbot asks a focused follow-up question when several products match or when an attribute is required.
Protect performance and catalog data
- Set a maximum page size and reject excessively long search strings.
- Cache repeated searches for a short period, while using shorter cache times for price and stock data.
- Apply rate limits per session, IP address, or authenticated integration.
- Return only the fields needed by the chatbot.
- Use HTTPS for every request and rotate API credentials regularly.
- Log searches and failures without recording unnecessary personal information.
- Test searches for hidden, password-protected, draft, backordered, and out-of-stock products.
Before deploying, test exact product names, SKUs, misspellings, synonyms, category filters, price ranges, variable products, and products with HTML-heavy descriptions. The goal is not merely to make the catalog searchable; it is to ensure that every answer is based on products the customer can actually view and, where appropriate, purchase.