How to remove an item from Shopping cart via CartJS for Virtual Agent

Randell Anders
Tera Contributor

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?

1 ACCEPTED SOLUTION

Rao Vamshi
Kilo Guru

Hi @Randell Anders 

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:

RaoVamshi_2-1682352081663.png


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:

 

RaoVamshi_3-1682352111389.png

 

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

View solution in original post

4 REPLIES 4

Rao Vamshi
Kilo Guru

Hi @Randell Anders 

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:

RaoVamshi_2-1682352081663.png


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:

 

RaoVamshi_3-1682352111389.png

 

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

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?

Hi @Randell Anders 

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:

RaoVamshi_1-1682355271143.png

 

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

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:

 

AdinanB_0-1748549143821.png

 

But I'm getting an error as the response, saying that the method removeItem doesn't exists:

 

AdinanB_1-1748549208857.png

 

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.