πŸ“£ The forum has moved to GitHub

We’ve switched to GitHub Discussions as the hub for our community. This will improve the experience of collaborating for everyone, sharing what you’re working on, and discussing ideas for how Swell can be improved. Existing threads will remain open, but new posts are disabled.

Go to GitHub β†’

How to get category details for each up_sell product ?

I need to get up_sell products' categories while fetching the given product details. Currently, the given product details object contains a up_sells array with only id and product_id and I'm able to expand the product object also for each upsell by the following query.

But inside the product object for each up_sell item, I'm not able to get its category details and I can't also expand its category by using expand: [ 'up_sells.product.category']

Answers

  • @Gaurav Pandey Brilliant question and nice attempt.

    You should have tried 'up_sells.product.categories'Β to expand the categories for each up_sell item.

    Hope this answers your question. If not, please reply to this thread and I am happy to answer you.

  • @Logesh_Dinakaran I had tried this also, but it didn't work. Throws the permission error.


  • Logesh_Dinakaran
    edited September 2022

    @Gaurav Pandey Sorry, I assumed that you were testing in swell-node and the previous answer would work with backend APIs and not in swell-js (or frontend APIs)

    You cannot fetch more than 1-level deep using swell-js. But, there's a workaround for this and please let me know whether this works for you.

    response = await swell.categories.list({
      "id": {
        "$in": ["category_id_1", "category_id_2"]
      }
    });
    

    I guess that you may already explore this option as well, but this is the only option available at the moment for swell-js. I would also like to understand your use case for this requirement so that I can check with my product team whether we could include this pattern in the query permissions.

  • hey @Logesh_Dinakaran this only fetches a list of categories. I am looking for category details while expanding the up_sells products list in a given product object.

    My use-case is pretty straightforward - I want category details to appear in up_sells products because, in the UI, I am showing up_sell products cards (as related products).

    My product details page URL has category_slug for SEO purposes.

    Now each up_sell product card links to its details page and this link is broken now due to missing category details.

  • @Gaurav Pandey Thanks for sharing your use case. So, you want to fetch a list of upsell products for a given product and each upsell product should navigate to a URL generated using upsell product's category slug.

    If my understanding is correct, please follow the below steps:

    1. Fetch a list of upsell products for a given product

    let productDetail = await swell.products.get('product_id', {
      expand: 'up_sells.product'
    })
    

    2. Load category details for the available upsell products.

    // Fetch the unique category_ids for the available upsell products.
    let categories = new Set();
    productDetail.upSells.forEach(upSell => {
      upSell.product.categoryIndex.id.forEach(categoryId => categories.add(categoryId))
    })
    
    // Lookup category details for those unique category_ids via a single query
    let categoryList = await swell.categories.list({
      'id': {
        '$in': Array.from(categories)
      }
    })
    
    let categoryMapper = {}
    categoryList.results.forEach(category => {
      categoryMapper[category.id] = category
    })
    

    3. Display the upsell items with a corresponding category-slug.

    productDetail.upSells.forEach(upSell => {
      upSell.product.categoryIndex.id.forEach(categoryId => {
        let productUrl = `https://store_id.swell.store/products/${categoryMapper[categoryId].slug}`
        console.log(productUrl)
      })
    });
    

    Hope this answers your question. If not, please reply to this thread and I am happy to answer you.

Sign In or Register to comment.