vercel / sveltekit-commerce

SvelteKit Commerce

Home Page:https://sveltekit-commerce.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search functionality does not work due to API limitations

ApalloAltoBluck opened this issue · comments

In the getAllProducts() function, there is no logic to support Shopify's pagination requirement.

As a result, the store can only search through the first 250 products.

For anyone with the same issue, I added a new function to shopify JS like this --

export async function getProductsByTitle(title) {
  
  let query = `
  {
    products(first:25, query: "title:${title}*") {
      edges {
        node {
          id
          handle
          title
          priceRange {
            maxVariantPrice {
                amount
                currencyCode
            }
            minVariantPrice {
                amount
                currencyCode
            }
        }
          images(first: 1) {
            edges {
              node {
                originalSrc
                altText
                width
                height
              }
            }
          }
        }
      }
     
    }
  }
  `

  return shopifyFetch({
    query: query,
  })
}

This doesn't scale if there are a lot of products because of API limitations, but this provides a partial solution to a more accurate search functionality. Maybe consider looking into the Bulk operations to get a real answer to this.