WPSolr logo
Search
Close this search box.

Table of contents :

How to get started with Weaviate in WooCommerce

wpsolr-header-solr-elasticsearch-5

Table of contents :

Introduction:

Weaviate is an open-source vector search engine that allows you to build smart search applications. If you are using WooCommerce to power your online store, you can enhance your search capabilities by integrating Weaviate. In this post, we will show you how to get started with Weaviate in WooCommerce.

How to Get Started with Weaviate in WooCommerce:

To get started with Weaviate in WooCommerce, you will need to install the Weaviate PHP client. You can install it using Composer, which is a dependency manager for PHP.

Once you have installed the Weaviate PHP client, you will need to create a Weaviate instance and an index. You can do this using the following code:

// Include the Weaviate PHP client
require_once 'vendor/autoload.php';

use Weaviate\Client\Configuration;
use Weaviate\Client\Api\ThingsApi;
use Weaviate\Client\Model\Thing;

// Create a Weaviate configuration object
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://localhost:8080/v1');

// Create a Weaviate API client
$client = new ThingsApi(
    new Swagger\Client\ApiClient($config)
);

// Create a Weaviate instance and index
$instance = 'my-weaviate-instance';
$index = 'my-weaviate-index';

$client->thingsPost(new Thing([
    'class' => 'MySchemaClass',
    'schema' => [
        'name' => 'John Doe'
    ]
]), $instance, $index);

This code creates a Weaviate configuration object and API client, sets the Weaviate instance and index, and creates a new instance of a MySchemaClass with a name property set to “John Doe”.

Once you have created the Weaviate instance and index, you will need to index your WooCommerce products in Weaviate. To do this, you can use the WooCommerce REST API to retrieve your products and then send them to Weaviate using the Weaviate PHP client. Here is an example of how you can do this:

// Include the WooCommerce API client
require_once 'vendor/autoload.php';

use Automattic\WooCommerce\Client;

// Create a WooCommerce API client
$woocommerce = new Client(
    'https://example.com',
    'consumer_key',
    'consumer_secret',
    [
        'wp_api' => true,
        'version' => 'wc/v3',
    ]
);

// Retrieve all products from WooCommerce
$products = $woocommerce->get('products');

// Include the Weaviate PHP client
require_once 'vendor/autoload.php';

use Weaviate\Client\Configuration;
use Weaviate\Client\Api\ThingsApi;
use Weaviate\Client\Model\Thing;

// Create a Weaviate configuration object
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://localhost:8080/v1');

// Create a Weaviate API client
$client = new ThingsApi(
    new Swagger\Client\ApiClient($config)
);

// Send each product to Weaviate
foreach ($products as $product) {
    $id = $product['id'];
    $name = $product['name'];

    $client->thingsPost(new Thing([
        'class' => 'MySchemaClass',
        'schema' => [
            'id' => $id,
            'name' => $name
        ]
    ]), $instance, $index);
}

This code retrieves all products from WooCommerce using the WooCommerce API client, and then sends each product to Weaviate using the Weaviate PHP client.

Once you have indexed your products in Weaviate, you can search for them using the Weaviate API. Here is an example of how you can search for products in Weaviate:

// Include the Weaviate PHP client
require_once 'vendor/autoload.php';

use Weaviate\Client\Configuration;
use Weaviate\Client\Api\ThingsApi;
use Weaviate\Client\Model\SearchResults;

// Create a Weaviate configuration object
$config = Configuration::getDefaultConfiguration()
    ->setHost('https://localhost:8080/v1');

// Create a Weaviate API client
$client = new ThingsApi(
    new Swagger\Client\ApiClient($config)
);

// Search for products in Weaviate
$searchResults = $client->thingsSearch($instance, $index, [
    'query' => [
        'should' => [
            'or' => [
                [
                    'path' => 'name',
                    'value' => 'Headphone'
                ],
                [
                    'path' => 'name',
                    'value' => 'Speaker'
                ]
            ]
        ]
    ]
]);

// Print the search results
$results = $searchResults->getResults();
foreach ($results as $result) {
    $thingId = $result->getId();
    $name = $result->getSchema()['name'];

    echo "Thing $thingId has name $name.\n";
}

This code searches for products in Weaviate that have a name matching either “Headphone” or “Speaker” and prints the search results.

How WPSOLR Can Help:

WPSOLR is a WordPress plugin that can help you integrate Weaviate into your WooCommerce store. WPSOLR provides a powerful search engine for WooCommerce that supports Weaviate integration out of the box. With WPSOLR, you can easily configure and customize your search experience, including search fields, filters, and sort options. Additionally, WPSOLR provides advanced search features, such as fuzzy search, autocomplete, and suggestions.

Conclusion:

Weaviate is a powerful search engine that can enhance your WooCommerce store’s search capabilities. With the Weaviate PHP client, you can easily index and search for your WooCommerce products in Weaviate. Additionally, with WPSOLR, you can integrate Weaviate into your WooCommerce store and provide a powerful search experience for your customers.

Related posts ... not powered by WPSOLR 😊