Customizing Apache Solr to work with WooCommerce Product Variations

Table of Contents

Introduction

Apache Solr is a powerful search engine that is widely used to enhance the search capabilities of web applications. One popular use case is integrating Solr with WooCommerce, an open-source e-commerce platform built on WordPress. WooCommerce provides support for product variations, which allow customers to select different options for a particular product. However, out of the box, Solr does not support indexing and searching for product variations. In this post, we will explore how to customize Apache Solr to work seamlessly with WooCommerce product variations.

Customizing Apache Solr for WooCommerce Product Variations

To start, we need to ensure that Solr is properly configured and running on our server. Once that is done, we can begin customizing the indexing and searching process to include product variations.

Indexing Product Variations

The first step is to modify the indexing process to include product variations. WooCommerce stores product variations as individual products in the database. We need to make sure that all the relevant data related to these variations is indexed by Solr.

To achieve this, we can use the WooCommerce REST API to fetch the list of all products and their variations. Once we have this data, we can loop through each product and its variations, extracting the required attributes such as price, stock status, SKU, and any custom attributes. We can then pass this data to Solr for indexing.

Here’s an example of how we can achieve this using the PHP client for Solr:


// Fetch list of WooCommerce products using REST API
$products = get_wc_products();

// Loop through each product
foreach ($products as $product) {
    // Fetch variations for the product
    $variations = get_wc_variations($product->id);

    // Loop through each variation
    foreach ($variations as $variation) {
        // Extract variation attributes
        $price = get_wc_variation_price($variation->id);
        $stock_status = get_wc_variation_stock_status($variation->id);
        $sku = get_wc_variation_sku($variation->id);
        $custom_attributes = get_wc_variation_custom_attributes($variation->id);

        // Create Solr document
        $document = new SolrInputDocument();
        $document->addField('id', $variation->id);
        $document->addField('product_id', $product->id);
        $document->addField('price', $price);
        $document->addField('stock_status', $stock_status);
        $document->addField('sku', $sku);
        $document->addField('custom_attributes', $custom_attributes);

        // Add document to Solr index
        $solr->addDocument($document);
    }
}

// Commit changes to Solr index
$solr->commit();

Searching for Product Variations

Once we have indexed the product variations, we can modify the search process to include them as well. By default, Solr will only return the parent product in the search results. We need to customize the search query to include the variations and display them as separate results.

To achieve this, we need to modify the Solr query to search for both the parent product and its variations. We can use the “product_id” field, which we added during the indexing process, to identify the parent and its variations. We can then retrieve the necessary data from Solr and display it on the search results page.

Here’s an example of how we can modify the Solr query using the PHP client:


// Search query
$query = new SolrQuery();
$query->setQuery('keyword:example');
$query->setRows(10);

// Filter query to include only parent products and their variations
$query->addFilterQuery('product_id:(123 OR 456)');

// Execute Solr query
$response = $solr->query($query);

// Parse and display search results
foreach ($response->getResponse()->response->docs as $doc) {
    // Display parent product details
    echo 'Parent Product: '.$doc->id;

    // Fetch and display variation details
    $variations = get_wc_variations($doc->product_id);
    foreach ($variations as $variation) {
        echo 'Variation: '.$variation->id;
        // Display variation details
    }
}

How WPSOLR Can Help

While customizing Apache Solr for WooCommerce product variations can be a complex task, there are tools available that can simplify the process. WPSOLR is a plugin specifically designed to integrate Solr with WordPress, including WooCommerce. It provides an intuitive interface to configure Solr indexing, searching, and faceted navigation.

WPSOLR allows you to define custom fields for indexing product variations, making it easy to map the relevant attributes. Additionally, it provides a ready-to-use search template that handles the display of product variations in the search results.

By leveraging the capabilities of WPSOLR, you can save time and effort in customizing Solr for WooCommerce product variations, and focus more on delivering a seamless search experience to your customers.

Conclusion

Customizing Apache Solr to work with WooCommerce product variations can greatly enhance the search capabilities of your e-commerce website. By properly indexing and searching for variations, you can provide your customers with a more tailored and efficient shopping experience.

With the help of the PHP client for Solr, you can easily customize the indexing and searching process to include product variations. And by using WPSOLR, you can simplify the entire process and take advantage of its powerful features to seamlessly integrate Solr with WooCommerce.

So, whether you choose to customize Solr on your own or leverage the capabilities of WPSOLR, you can ensure that your customers can easily find the products they’re looking for, even when dealing with complex product variations.

Read more related content

ūüĎć The powerful¬†Vespa.ai¬†finally within everyone’s reach

Small business WooCommerce¬†shops finally have access to its world class search. Here is the first preview of¬†WooCommerce¬†demo with¬†Vespa.ai¬†search¬†https://demo-woocommerce-flatsome-cloudways-2k-vespa.wpsolr.com/shop/¬†! – Perfect for Vespa’s newbies, no requirement