Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commercetools] Product preview is limited to 20 products #1592

Closed
img-alc opened this issue Aug 9, 2022 · 1 comment
Closed

[commercetools] Product preview is limited to 20 products #1592

img-alc opened this issue Aug 9, 2022 · 1 comment

Comments

@img-alc
Copy link

img-alc commented Aug 9, 2022

Issue description
Adding more than 20 products to a field leads to "Product missing" message in products preview

Steps to reproduce

  1. Create a new entry (content type must have a field with commerce tools app installed for products widget)
  2. Add more than 20 products to the field connected to commerce tools
  3. Save
  4. Only 20 products are properly shown, while exceeding products show the message mentioned before

Screenshot
image

Expected behavior
I expected all selected products to show correctly in the preview, the error message is misleading. The products do exist and should render (name & image).

Root cause
Commerce tools api response is limited to 20 entries. You have to provide the offset in order to fetch remaining results. Example of a response:
image

The logic to fetch the products preview is not considering the number of results, always returning at most 20 entries (link).

export async function fetchProductPreviews(
  skus: string[],
  config: ConfigurationParameters
): Promise<Product[]> {
  if (skus.length === 0) {
    return [];
  }

  const client = createClient(config);
  const response = await client
    .productProjections()
    .search()
    .get({
      queryArgs: {
        'filter.query': [`variants.sku:${skus.map((sku) => `"${sku}"`).join(',')}`],
      },
    })
    .execute();

  if (response.statusCode === 200) {
    const products = response.body.results.map(productTransformer(config));
    const foundSKUs = products.map((product: Product) => product.sku);
    const missingProducts = skus
      .filter((sku) => !foundSKUs.includes(sku))
      .map((sku) => ({
        sku,
        image: '',
        id: '',
        name: '',
        isMissing: true,
      }));
    return [...products, ...missingProducts];
  }
  throw new Error(`Request failed with status ${response.statusCode}`);
}
@colomolo
Copy link
Contributor

colomolo commented Sep 7, 2022

@img-alc Thanks for opening the issue. We now fetching max possible amount of products. Should be enough for most cases.

@colomolo colomolo closed this as completed Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants