Developer forum

Forum » CMS - Standard features » Cookie manager and cookies named after user IDs

Cookie manager and cookies named after user IDs

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

As part of debugging the issue with the disappearing carts (https://doc.dynamicweb.com/forum/ecommerce-standard-features/ecommerce-standard-features/losing-carts-for-logged-in-users), I found some other weirdness. With the disappearing cart, it seems to disappear when I don't have a cookie and the cart key disappears from the cache. Could it be that the cookie is never set because of my cookie opt-in level?

I looked into classifying the cart cookie as Functional, but then noticed this:

That seems that I have hundreds of cookies, one for each user on the site or so. Is that right? When I register Dynamicweb:Ecom:Cart as the main functional cookie will that work since it's actual use is with a user ID?

Imar


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

I don't think the issue is related to the general cookie handling or opt-in level, but you might want to check for any cookie issues reported by the browser.

If the cookie opt-in level is set to 0 (CookieOptInLevel.None) then no cookies will be set. So if the "Dynamicweb.CookieOptInLevel" cookie has the value 0 then it might be causing the issue that you are experiencing. Otherwise, the cookie is treated as functional and will be set (unless you have added it to the tracking category or a custom category).

I think the issue here is that the cart ends up in an invalid state at some point (maybe just temporarily).
It can be difficult to detect those issues, but maybe you can try to add a notification subscriber for debugging?

Here is a subscriber with some basic debugging (you might want to include additional checks)... 

[Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Order.BeforeSave)]
public sealed class DebugCartNotificationSubscriber : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs args)
    {
        var notificationArgs = (Notifications.Ecommerce.Order.BeforeSaveArgs)args;
        var order = notificationArgs.Order;

        if (ExecutingContext.IsBackEnd())
            return;

        if (!order.IsCart)
            return;

        var currentUserId = User.GetCurrentFrontendUserId();
        var currentOrderContextId = Common.Context.CartContext?.Id;

        var logger = LogManager.System.GetLogger("DebugCart");

        void invalidCartError(string message)
        {
            var info = $"Invalid cart: {message} (Id: {order.Id}, AutoId: {order.AutoId}, StateId: {order.StateId}, UserId: {order.CustomerAccessUserId}, CurrentUserId: {currentUserId}, OrderContextId: {order.OrderContextId}, CurrentOrderContextId: {currentOrderContextId})";
            logger.Error(info);
        }

        if (order.Complete)
            invalidCartError("Complete");

        if (order.Deleted)
            invalidCartError("Deleted");

        if (!order.IsCartEditable)
            invalidCartError("Not editable");
    }
}
 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Oh, I can see that you have added the cart cookie to the custom "Functional" category. That means that the cookie will not be set, unless you accept that cookie category.
Can you try to see what happens if you remove cart cookies from all cookie categories?

Any cookie which is required/necessary for the application to work should not be added to any cookie category.

 

You must be logged in to post in the forum