Try our conversational search powered by Generative AI!

Profile_MigrateAnonymous

Vote:
 

When I set an authentication cookie the cart that an anonymous user created isn't migrated over to the logged in user.

I have a custom global.asax which inherits from EPiServer.Global. If I'm not misreading the documentation this should happen "automagically", or?

http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-Commerce1/751/Orders/Shopping-cart/

If an anonymous user adds items and then logs in, the previous cart associated with that logged-in user will be merged. This is done in the Global.asax, Profile_MigrateAnonymous event handler.

#82386
Mar 11, 2014 13:29
Vote:
 

Hi,

I'm not sure about your question, but to make the Profile_MigrateAnonymous run automatically, you need the ProfileModule registered as a module in web.config (it should be there in default installation):

in system.webServer/modules:

      <add name="ProfileModule" type="EPiServer.Business.Commerce.HttpModules.ProfileModule, EPiServer.Business.Commerce" />

/Q

 

#82420
Mar 12, 2014 8:05
Vote:
 

Hi Quan,

Yes I have the module registered in web.config.

Before I sign in the user "cartHelper.LineItems" contains 1 item, after signing in the user with "FormsAuthentication.SetAuthCookie(username, remember);" and a page reload "cartHelper.LineItems" contains 0 items.

cartHelper is an instance of "new CartHelper("CustomCartName")"

#82423
Mar 12, 2014 8:46
Vote:
 

Hi,

I suggest to use this instead:

cartHelper = new CartHelper(Mediachase.Commerce.Orders.Cart.DefaultName);

"CustomCartName" is not a defined value in eCF API:s, is it your custom value? With the DefaultName cart, the cart will be automatically "bind" to current customer contact id.

Regards.

/Q

 

 

#82424
Mar 12, 2014 8:56
Vote:
 

Hi,

Yes it is a custom value since we have multiple carts on the site and different checkout processes depending on where the user is at the site. Yhe carts cannot be mixed togeather. So you are saying that the cart only migrates if I use the Mediachase.Commerce.Orders.Cart.DefaultName constant?

#82425
Mar 12, 2014 8:59
Vote:
 

Hi,

Currently, yes. But you can easily override/extend the behavior. Given you already register the event in Global.asax, you need to add a method like this:

        static void MigrateCart(string cartName, Guid contactId, string anonymousId, IMarket market)
        {
            var cart = new CartHelper(cartName, contactId, market);
            var anonymousCart = new CartHelper(cartName, new Guid(anonymousId), market);

            // Only perform merge if cart is not empty
            if (!anonymousCart.IsEmpty)
            {
                if (cart.IsEmpty)
                {
                    cart.OrderForm.AuthorizedPaymentTotal = anonymousCart.OrderForm.AuthorizedPaymentTotal;
                    cart.OrderForm.CapturedPaymentTotal = anonymousCart.OrderForm.CapturedPaymentTotal;
                }

                // Merge cart
                cart.Cart.Add(anonymousCart.Cart, true);
                cart.Cart.BillingCurrency = anonymousCart.Cart.BillingCurrency;
                cart.Cart.AcceptChanges();

                // Delete anonymous cart
                anonymousCart.Cart.Delete();
                anonymousCart.Cart.AcceptChanges();
            }
        }

Then create an event handler which migrates the cart(s) you want and register it the login event

I agree that this approach should be included by default and we will seek if we can improve the situation.

Regards.

/Q

#82426
Mar 12, 2014 9:06
Vote:
 

Hi again Quan,

Before migrating the OrderForms collection contains one item but after migration in the new cart the OrderForms collection contains two items and the tax calculation doesn't work.

If I remove the following lines the OrderForms collection just contains one item and the tax calculation works...

if (cart.IsEmpty)
{
    cart.OrderForm.AuthorizedPaymentTotal = anonymousCart.OrderForm.AuthorizedPaymentTotal;
    cart.OrderForm.CapturedPaymentTotal = anonymousCart.OrderForm.CapturedPaymentTotal;
}

is it a bug since the code looks the same as in EPiServer.Business.Commerce.HttpModules.ProfileModule?

Is it ok to remove these lines or will it mess up something else that I don't see right now?

#82466
Edited, Mar 13, 2014 9:12
Vote:
 

Hi,

That sounds strange. That code is to assign AuthorizedPaymentTotal and CapturedPaymentTotal, which should not affect the processing of the cart, as those value should be calculated later when you run some workflows (such as CartPrepare, CartCheckout). I would say it's safe to remove those code - and my apology for showing some code without checking it carefully.

Regards,

/Q

#82511
Mar 14, 2014 3:49
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.