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

BUG with the PUT method on the cart items api while trying to update the metadata list prop #159

Open
danielmariz opened this issue Feb 15, 2024 · 12 comments
Labels
bug Something isn't working

Comments

@danielmariz
Copy link

danielmariz commented Feb 15, 2024

Hi team, I'm getting a weird issue while trying to update a product variant metadata but this occurs only on the second product variant added to the cart.
My scenario requires to send a custom metadata for a single variant so I'm using the PUT method to update when a client add a new option for the same variant

When adding the first product variant to the cart everything works fine:

  1. cart is empty as you can see in the api response
    image
  2. now I added the first item via post method
    image (1)
  3. the payload is sent correctly
    image (2)
  4. and the response is also as expected, so it added an item with the metadata
    image (3)
  5. now I'm adding a new item to the same variant on a PUT method for the item id
    image (4)
  6. the payload is sent as documented using the $set object to override the whole metadata list
    image (5)
  7. the response is also correct the metadata it's being updated as expected
    image (6)
  8. now I'm gonna add a new product, the new product is also added using the POST method on the cart api
    image (7)
  9. the payload on the post method does not use the $set attribute
    image (8)
  10. the response is correct with a new item id on the cart and correct metadata
    image (9)
  11. now I'm adding a new metadata item for the same variant using the item id, it uses the PUT method
image
  1. the payload is sent using the $set prop again
image
  1. but the response does not change the metadata but changes the quantity
image

So in resume, the same calls with different behaviour from the api, I believe that using metadata like that is not a common pattern for you so you never caught this scenario before but is a requirement from my client because they don't want to treat sizes as a variant because will be more than 30 sizes combined with other 4 variant types and will be hard to manage the product content and prices. The issue only happens after the second product variant is added to the cart although I'm managing the first item with the same calls

@logeshswell
Copy link

From my understanding, the issue is PUT method on the cart items API is not updating metadata values correctly for the second cart item.

the payload is sent using the $set prop again

Can you share the correct screenshot of the request payload sent in Step-10?

@logeshswell logeshswell added the need-more-info Further information is requested label Feb 26, 2024
@danielmariz
Copy link
Author

From my understanding, the issue is PUT method on the cart items API is not updating metadata values correctly for the second cart item.

the payload is sent using the $set prop again

Can you share the correct screenshot of the request payload sent in Step-10?

I believe you are asking about the step-11 which was duplicated from step-10 now I updated the screenshots from step-11 simulating updating the metadata for a second variant added in the cart. So the first variant works fine with the same approach and from the second variant the issue start to happen

@logeshswell
Copy link

Can you try passing the request payload as follows for the PUT method?

{
  "$set": {
    "metadata": {
      "sizes": []
    }
  }
}

It is not highly recommended to pass $set field in the middle of the payload.

@logeshswell logeshswell removed the need-more-info Further information is requested label Feb 26, 2024
@danielmariz
Copy link
Author

danielmariz commented Feb 26, 2024

Can you try passing the request payload as follows for the PUT method?

{
  "$set": {
    "metadata": {
      "sizes": []
    }
  }
}

It is not highly recommended to pass $set field in the middle of the payload.

I've tried this approach but the docs seems to be outdate according to this comment

When trying as suggested I get a response error

image image

I believe the issue is in the api itself, we might need to get the swell api team involved as well

@logeshswell
Copy link

Sorry, I was wrong with the request payload structure as I didn't notice that it's a frontend API.
Can you try passing the request payload as follows?

{
  "metadata": {
    "$set": {
      "sizes": []
    }
  }
}

I will also ensure my team to update the documentation:
https://developers.swell.is/frontend-api/carts#update-cart-metadata

@danielmariz
Copy link
Author

Sorry, I was wrong with the request payload structure as I didn't notice that it's a frontend API. Can you try passing the request payload as follows?

{
  "metadata": {
    "$set": {
      "sizes": []
    }
  }
}

I will also ensure my team to update the documentation: https://developers.swell.is/frontend-api/carts#update-cart-metadata

the way you're suggesting also has issues

  1. the first call for the variant is a POST on cart items api with the payload you suggested
image
  1. the response for that returns the payload as expected
image
  1. the second call for the same variant is a PUT on the cart item id api with the payload updating the sizes
image
  1. the response for it changes the metadata shape so not consistent and hard to predict
image

so if you check the metadata object in the first call it returns sizes as a metadata property, but the second (update) puts the sizes inside an inner object with a key 0

When I tried to wrap the whole metadata object in the $set format than I could get it updated when adding items to the sizes array but not when removing an item, in that case the quantity changes but the sizes returns the removed index anyways. So still the api inconsistent

:/

@danielmariz
Copy link
Author

The swell team needs to investigate this scenario when updating an item metadata containing a list by adding and removing items from this list from multiple variants in the cart items

@logeshswell
Copy link

I am investigating this issue and will help you with the examples of adding and removing items from the metadata list field.

@logeshswell
Copy link

I can also reproduce this issue in my store and I can confirm that it is a bug.
We'll take a deeper look at this issue and get back to you if I get some updates.

@logeshswell logeshswell added the bug Something isn't working label Feb 28, 2024
@logeshswell
Copy link

@danielmariz Just for your information, the way you updated metadata.sizes field in the PUT request is correct as per your ticket description.

You can ignore the alternate suggestions provided by me about the usage of $set.

@danielmariz
Copy link
Author

Hi @logeshswell is there any plans for this fix in the short term? We're expecting on shipping to production soon so we might need to refactor on our side in order to make it work for us. Cheers!

@logeshswell
Copy link

This issue has already been prioritized and is yet to be worked on. I don't have an ETA, but I will keep you posted if I get any updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants