Product Recommendations from GraphQL Storefront API alway returning empty array

lukebennett
Tourist
3 0 1

So I've been on the Shopify Storefront docs and I found this.

I looks like I can get recommended products by providing a product id.

I've written the query below, but it always returns an empty array:

import { gql } from '@ts-gql/tag';

import type { Client } from '../utils/apollo-client';

export const PRODUCT_RECOMMENDATIONS = gql`
  query productRecommendations(
    $productId: ID!
    $variantsFirst: Int = 1
    $imagesFirst: Int = 5
  ) {
    productRecommendations(productId: $productId) {
      description
      handle
      id
      images(first: $imagesFirst) {
        edges {
          node {
            id
            originalSrc
            altText
          }
        }
      }
      priceRange {
        maxVariantPrice {
          amount
        }
        minVariantPrice {
          amount
        }
      }
      productType
      tags
      title
      variants(first: $variantsFirst) {
        edges {
          node {
            sku
            availableForSale
            id
            compareAtPriceV2 {
              amount
              currencyCode
            }
            priceV2 {
              amount
              currencyCode
            }
            title
            image {
              id
              originalSrc
              altText
            }
          }
        }
      }
    }
  }
` as import('../../../__generated__/ts-gql/productRecommendations').type;

export type ProductRecommendations =
  typeof PRODUCT_RECOMMENDATIONS['___type']['result']['productRecommendations'];

export async function getProductRecommendations(
  client: Client,
  productId: string,
  variantsFirst?: number,
  imagesFirst?: number
): Promise<ProductRecommendations | void> {
  const { data } = await client.query({
    query: PRODUCT_RECOMMENDATIONS,
    variables: {
      productId,
      variantsFirst,
      imagesFirst,
    },
  });
  return data?.productRecommendations;
}

 

 Is this because I'm using the Shopify Lite plan? I'd really like to use this feature if possible for a headless Shopify site that I'm building with Next.js.

Replies 2 (2)
dmytro_kushnir
New Member
2 0 0

Hello @lukebennett! Have you succeeded with Product Recommendations with StoreFront API? 

I'm trying to use such SDK: https://www.npmjs.com/package/shopify-buy#builds

but it seems it doesn't support the Product Recommendation method.

lukebennett
Tourist
3 0 1

Best I can tell it's not available unless you're on the Shopify Plus plan:

https://shopify.dev/themes/product-merchandising/recommendations#requirements-and-limitations

Pretty sure I've seen product recommendations used in themes before, but I guess they used a different API...