Skip to main content

useSearchProductRecommendations

The useSearchProductRecommendations hook is a specialized wrapper around useFetch used to fetch product recommendations. It connects to the search provider's recommendation engine (e.g., Coveo or SAP Commerce Machine Learning) to provide personalized product suggestions based on a product code, cart contents, or user history. It works on the client as well as on the server.

import { useSearchProductRecommendations } from '@archibald/search';

const { data: recommendations, isLoading } = useSearchProductRecommendations(options, fetchOptions);

Parameters

NameTypeRequiredDescription
optionsSearchRecommendationsRequestOptions✔️Options for the recommendations request.
fetchOptionsFetchMinOptionsStandard options for the useFetch hook.

options

  • Type: SearchRecommendationsRequestOptions
PropertyTypeDescription
slotIdstringThe ID of the recommendation slot (required).
productIdstringOptional product code to base recommendations on.
cartSearchRecommendationsCartAndPurchased[]Optional current cart items for better recommendations.
purchasedSearchRecommendationsCartAndPurchased[]Optional historical purchased items.

fetchOptions

Standard data fetching options. See useFetch for more information.

Return value

  • Type: FetchResult<SearchRecommendationResponse>
PropertyTypeDescription
dataSearchRecommendationResponse | nullThe recommendations data, including product list and headline.
isLoadingbooleanTrue if the request is in progress.
isDonebooleanTrue if the request has finished.

Example

import { useSearchProductRecommendations } from '@archibald/search';

function RelatedProducts({ currentProductCode }) {
const { data, isLoading } = useSearchProductRecommendations({
slotId: 'pdp-related-products',
productId: currentProductCode
});

if (isLoading || !data?.products?.length) return null;

return (
<section>
<h4>{data.headline || 'You may also like'}</h4>
<ProductCarousel products={data.products} />
</section>
);
}