Skip to content

Product recommendations with our PHP client

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

Start by making an Recommender-instance.

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

$recommender = new Recommender(
  "00000000-0000-0000-0000-000000000001", 
  "your api key",
$recommender->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\PopularProductsRequest;

Before going any further, make sure you have read the following:

  • Read about how to authenticate against our API here.
  • Read about handling different types of users here.

Also remember to specify your Server URL in your request.

Product Details Page (PDP)

On the Product Details Page (PDP), the most commonly used recommendations are PurchasedWithProduct and ProductsViewedAfterViewingProduct. Here are examples on how to implement those requests. We recommend that you batch the request into a single request. Read more about why and how here.

PurchasedWithProduct

php
$id = ProductAndVariantId::create("productId", "variantId");

$request = PurchasedWithProductRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  "Product details page",
  $this->getUser(),
  $id
);

$request->setSettings(
  ProductRecommendationRequestSettings::create()
    ->setSelectedProductProperties(
      SelectedProductPropertiesSettings::create()
        ->setDisplayName(true)
    )
    ->setSelectedVariantProperties(
      SelectedVariantPropertiesSettings::create()
        ->setDisplayName(true)
    )
);

$response = $recommender->purchasedWithProduct($request);

ProductsViewedAfterViewingProduct

php
$id = ProductAndVariantId::create("productId", "variantId");

$request = ProductsViewedAfterViewingProductRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  "Product details page",
  $this->getUser(),
  $id
);

$request->setSettings(
  ProductRecommendationRequestSettings::create()
    ->setSelectedProductProperties(
      SelectedProductPropertiesSettings::create()
        ->setDisplayName(true)
    )
    ->setSelectedVariantProperties(
      SelectedVariantPropertiesSettings::create()
        ->setDisplayName(true)
    )
);

$response = $recommender->productsViewedAfterViewingProduct($request);

By using SelectedProductPropertiesSettings or SelectedVariantPropertiesSettings it is posible to select the specific properties, from Relewise, that you need for rendering a product tile, list etc.. That way we can store fields that make the filtering or merchandising better, but maybe is not needed when rendering the actual product. So we can reduce the HTTP responses by only returning a minimum set of the information that is needed.

Power Step

On the Power Step the most used recommendation is PurchasedWithProduct.

php
$id = ProductAndVariantId::create("productId", "variantId");

$request = PurchasedWithProductRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  "Power Step overlay",
  $this->getUser(),
  $id
);

$request->setSettings(
  ProductRecommendationRequestSettings::create()
    ->setSelectedProductProperties(
      SelectedProductPropertiesSettings::create()
        ->setDisplayName(true)
    )
    ->setSelectedVariantProperties(
      SelectedVariantPropertiesSettings::create()
        ->setDisplayName(true)
    )
);

$response = $recommender->purchasedWithProduct($request);

Cart / Basket Page

On the Basket Page the most commonly used recommendation is the PurchasedWithMultipleProducts. This algorithm works for both identified and anonymous users, making it easy to use a recommendation slider on your basket page to boost your conversions.

php
$id1 = ProductAndVariantId::create("123456789", "variantId1");
$id2 = ProductAndVariantId::create("987654321", "variantId2");

$request = PurchasedWithMultipleProductsRequest::create(
  Language::create("da"),
  Currency::create("DKK"),
  "Product details page",
  $this->getUser()
);

$request->setProductAndVariantIds($id1, $id2);

$response = $recommender->purchasedWithMultipleProducts($request);

On the Category Page (PLP), the most commonly used recommendation is PopularProducts. Here is an example on how to implement this request.

php
$popularProducts = PopularProductsRequest::create(
    Language::create("da"),
    Currency::create("DKK"),
    "Category Page",
    $this->getUser(),
    PopularityTypes::MostPurchased
)->setFilters(
    FilterCollection::create(
        ProductCategoryIdFilter::create(CategoryScope::Ancestor)
            ->setCategoryIds("c-1")
    )
);

$response = $recommender->popularProducts($popularProducts);

Batching recommendation requests.

It's best practice to batch all recommendation requests on a page into a single request against the Relewise API. This has the benefit of ensuring unique products between different recommendations - and also minimizing network traffic.

php
$request = ProductRecommendationRequestCollection::create(
  true, //require distinct products across results
  $purchasedWithProductRequest,
  $productsViewedAfterViewingProductRequest
);

$response = $recommender->batchproductRecommendation($request);

The example here uses product recommendations, but the same can be done for product categories, content, and content categories.

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