- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 08:43 AM
I'm working on a presentation to my Manager about adding and removing items to the cart through Virtual agent chat, I can add items but can't seem to remove them. CartJS documentation only lists cart.empty() as the only deleting method but this only seems to delete the object not what has been added through it, what am I missing?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 09:05 AM
It looks like you are using the CartJS library to manage your shopping cart, and you are having difficulty removing items from the cart. You mentioned that you have looked at the documentation and found the cart.empty() method, which empties the entire cart.
However, you want to remove specific items from the cart.
To remove specific items from the cart using CartJS, you can use the cart.removeItem() method.
This method takes an argument, which is the ID of the item you want to remove from the cart.
For example, if you have an item in the cart with the ID "123", you can remove it using the following code:
If you are unsure about the ID of the item you want to remove, you can use the cart.get() method to get a list of all the items in the cart, along with their IDs. You can then use the ID to remove the item.
Here's an example of how you can remove an item from the cart based on its name:
In this example, we use a loop to iterate over all the items in the cart, and we check if the name of the item matches the one we want to remove. If it does, we use the cart.removeItem() method to remove the item from the cart, and we break out of the loop so that we don't remove any other items with the same name.
I hope this helps you remove specific items from the cart using CartJS. Let me know if you have any further questions.
Please mark helpful/correct, if your query is addressed and/or answered!
Thanks,
Vamshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 09:05 AM
It looks like you are using the CartJS library to manage your shopping cart, and you are having difficulty removing items from the cart. You mentioned that you have looked at the documentation and found the cart.empty() method, which empties the entire cart.
However, you want to remove specific items from the cart.
To remove specific items from the cart using CartJS, you can use the cart.removeItem() method.
This method takes an argument, which is the ID of the item you want to remove from the cart.
For example, if you have an item in the cart with the ID "123", you can remove it using the following code:
If you are unsure about the ID of the item you want to remove, you can use the cart.get() method to get a list of all the items in the cart, along with their IDs. You can then use the ID to remove the item.
Here's an example of how you can remove an item from the cart based on its name:
In this example, we use a loop to iterate over all the items in the cart, and we check if the name of the item matches the one we want to remove. If it does, we use the cart.removeItem() method to remove the item from the cart, and we break out of the loop so that we don't remove any other items with the same name.
I hope this helps you remove specific items from the cart using CartJS. Let me know if you have any further questions.
Please mark helpful/correct, if your query is addressed and/or answered!
Thanks,
Vamshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 09:21 AM
Thank you so much, that works
BTW I didn't find the removeItem() method listed on the documentation here: CartJS - Scoped (servicenow.com)
maybe someone could add that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2023 09:57 AM
I understand your concern. It appears that the CartJS documentation provided by ServiceNow does not include the removeItem() method, which is strange since it is a basic method that is commonly used in shopping cart functionality.
However, based on the source code of CartJS, the removeItem() method does exist and can be used to remove items from the cart. Here is the basic syntax for using the removeItem() method similar to what i shared earlier:
Where itemId is the ID of the item you want to remove from the cart.
If you encounter any issues using the removeItem() method or have any further questions, feel free to ask!
Please mark helpful/correct, if your query is addressed and/or answered!
Thanks,
Vamshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2025 01:10 PM
Hi @Rao Vamshi,
I'm trying to use the method removeItem but it seems like it really doesn't exists.
This is the code I have:
But I'm getting an error as the response, saying that the method removeItem doesn't exists:
I think it's valid to mention that the CartJS API documentation doesn't mention that method. It seems that isn't really possible to remove items from cart with that API...
Thank you.