Skip to content

Filtering products in search or recommendations with our PHP client

The examples below are using our PHP SDK, which is available via Packagist.

In the examples we will be performing filtering on Search-request, but you can do the same for all recommendation requests. You will need an instance of our Searcher:

php
use Relewise\Searcher;
// You have to `use` the above to create a Searcher.

$searcher = new Searcher(
  "00000000-0000-0000-0000-000000000001", 
  "your api key",
$searcher->serverUrl = "the server URL for the dataset";

To make requests with the SDK you need to use the types from our PHP SDK. The types are located in the namespace Relewise\Models and an example of using a concrete type could be the following:

php
use Relewise\Models\ProductSearchRequest;

Also remember to specify your Server URL in your request.

It is recommended that you start by reading about product search.

The following shows an example on how to perform a product search with 3 selected facets.

Different assortments

A common scenario is handling different assortments. You might have products that are unique to specific markets or specific customers on the same website using Relewise. Here the assortments might be overlapping but not identical, so we need some way of only showing the products that are available in the given context. There are 2 different ways of handling this.

The first option is to use our Assortment feature, and specify an Assortment value for each market/customer (these have to be numbers) on all products.

php
Product::Create("productId")
  ->setAssortments(1, 2);

Then when performing a search you want to add a ProductAssortmentFilter to the search request.

php
$request = ProductSearchRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  $this->getUser(),
  "Search overlay",
  "<search-term>",
  skip: 0,
  take: 10
);

$request->setFilters(FilterCollection::create(ProductAssortmentFilter::create(1)));

$response = $searcher->productSearch($request);

If you are using Variants, and these also can be specific to market/customer then you can use the mechanism on variants and add an assortment to those.

Using the Data-bag to handle assortment

The second option is using the Products Data-bag to add a list of shops, where the product is available and then filter on those.

php
Product::create("productId")
  ->addToData(
      "AvailableOnMarkets",
      DataValueFactory::stringList("DK", "DE")
  );

Then when performing a search you want to add a ProductDataFilter to the search request.

php
$request = ProductSearchRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  $this->getUser(),
  "Search overlay",
  "gaffel",
  skip: 0,
  take: 10
);

$request->setFilters(
  FilterCollection::create(
    ProductDataFilter::create("AvailableOnMarkets", filterOutIfKeyIsNotFound: true)
      ->setConditions(
        ValueConditionCollection::create(
          ContainsCondition::create()
            ->setValue(DataValueFactory::string("DK"))
        )
      )
  )
);

$response = $searcher->productSearch($request);

In Stock

Hiding products that are out of stock, can be achieved by adding a InStock indicator on products, like so:

php
Product::create("productId")
  ->addToData(
    "InStock",
    DataValueFactory::boolean(true/false)
  );

Then when performing a search you want to add a ProductDataFilter to the search request.

php
$request = ProductSearchRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  $this->getUser(),
  "Search overlay",
  "gaffel",
  skip: 0,
  take: 10
);

$request->setFilters(
  FilterCollection::create(
    ProductDataFilter::create("InStock", filterOutIfKeyIsNotFound: true)
      ->setConditions(
        ValueConditionCollection::create(
          EqualsCondition::create(DataValueFactory::boolean(true))
        )
      )
  )
);

$response = $searcher->productSearch($request);

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com