Custom cart throws error for first time visitors

I am creating a custom cart and I want to check if the cart is empty and let the user know its empty using getCurrentCart(). It works fine for logged in users and visitors who have emptied existing carts but , any first time visitor clicking the cart page for the first time without having added any items results in this failed promise and console error

Console error message : “Cart with person identity 1e20ce17-2d77-4fc7-88de-c9c2a349db15 not found”

So the only way I can check if the cart is that of a first time visitor, is something like this


getCurrentCart()
.then((response) => {
 // Cart exists
}.
catch((error) => {
	if(error.message.startsWith('Cart with person identity')) { 
		// Show the visitor his cart is empty
	{
});

This is crude and possibly unreliable if the error message changes in future… Are there better ways of doing this?

Also, this error displays in the console even for the cart provided by wix if its empty and clicked on by a first time visitor

BTW , I am testing this on a live published version of the site and tried on 3 browsers in incognito and normal mode

1 Like

I don’t have an answer but why check for a specific error at this point and just assume that the error handler means the cart is empty. I did have a thought about assigning a visitor a role called say empty cart and if this role is assigned then the cart is empty.

when a product is added the cart will be created and an event fired And there you can assign a role called notemptycart. you Would have to check for no role assigned or a default role before doing the assignments. have not really thought through it but it might give you an idea. The problem is the cart does not exist when you check for it and maybe the error handler is the best approach. good luck

I assume there could be different types of errors that can be caught if getCurrentCart() fails, so i wanted a way to specifically know its an error because of an empty first time visitor cart. I don’t have issues with checking for an empty cart once a product is added, since the cart stays for the session. This is just for the absolute first time a visitor checking his cart. I was looking for some kind of object with an error code or number possibly to clearly show its an empty cart as opposed to a message like “Cart with person identity not found” because , if i checked it based on this string and it changed in future without my knowledge it could lead to issues. The error object doesn’t contain much else. Ill have a look at the roles options otherwise looks like this is the only to go for now. Thanks anyways

details:
[[Prototype]]: Object
errorGroup: "User"
message: "Cart with person identity db705ecd-20d9-43ff-b632-1f6e99ca0b6d not found"
[[Prototype]]: Object

Hola,
La solución cuando invocamos el método getCurrentCart() de wix-stores-backend y da el sigiuente error:
Cart with person identity 46ef0d8c-23d5-46f5-87dd-5142a87f7a86 not found

//Backend → archivo producto.jsw
import wixStoresBackend from ‘wix-stores-backend’

export function getCurrentCart ( ) {
return wixStoresBackend . getCurrentCart ();
}

//Cart Page
import { getCurrentCart } from ‘backend/productos.jsw’

$w . onReady ( function () {
getCurrentCart ()
. then ( result => { arguments
console . log ( result , ‘current cart’ )
})
});

El problema es que en el ambiente de ‘Vista Previa’ no funciona el código su totalidad, debes publicar y ver el sitio, en ese ‘ambiente’ verás los cambios (el console.log())

Saludos!