Developer forum

Forum » Development » Merge carts (temporary and active)

Merge carts (temporary and active)

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have a situation where it would be useful to be able to merge carts, I will explain.

I want to create a temporary cart on an Order Context that's different than the regular CartContext.
When I am done with making adjustments to the temporary cart, I want to be able to allow the user to select a cart and merge the content of the temporary cart into the selected cart.

How can I accomplish the above scenario in a decent manner (I can think about a few potential hacks but I would rather use a more standard approach if any).

Thank you,
Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

There are no standard approach - so the hacky approach using the API must be the way to go.

BR Nicolai

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I was hoping that the approach used when merging an anonymous cart with an authenticated cart would apply to this situation.

But I will use the hacky approach instead.

Thank you,
Adrian

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

I believe that is the hacky part :-):

This is the code that handles that:

foreach (Order cartBeforeLogin in cartsBeforeLogin)
                    {
                        if (cartBeforeLogin.IsCart)
                        {
                            if (string.IsNullOrEmpty(cartBeforeLogin.OrderContextId))
                            {
                                Common.Context.CartContext = null;
                            }
                            else
                            {
                                var cartContext = Services.OrderContexts.GetOrderContextById(cartBeforeLogin.OrderContextId);
                                if (cartContext is null)
                                {
                                    continue;
                                }

                                Common.Context.CartContext = cartContext;
                            }

                            if (SystemConfiguration.Instance.GetBoolean("/Globalsettings/Ecom/Cart/MergeAnonymousCartOnLoggingIn"))
                            {
                                // Remove anonymous cart cookies
                                Services.Carts.ClearAnonymousCart(null, Common.Context.CartContext);
                                var userCart = Common.Context.Cart;
                                if (userCart is object && ((userCart.Id ?? "") != (cartBeforeLogin.Id ?? "")))
                                {
                                    foreach (OrderLine orderLineToCopy in cartBeforeLogin.OrderLines)
                                        Services.OrderLines.AddOrderLineCopy(userCart, orderLineToCopy, true);
                                }
                                else
                                {
                                    Common.Context.SetCart(cartBeforeLogin);
                                }
                            }
                            else
                            {
                                Common.Context.SetCart(cartBeforeLogin);
                            }
                        }
                    }
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

it is a very good hack. I could definitely use some of that logic.

Thank you,

Adrian

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Alternatively, you should be able to handle it in a template...
Render the temporary cart product lines (productid, unitid, quantity, etc.) in a form with cartcmd=addmulti and cartid={Id} - where id is the identifier of the cart you want to add the lines to when the form is posted.
You can also use cartcmd=setcart&cartid={Id} if you need to switch the current cart or cartcmd=archive&cartid={Id} if you need to remove a cart completely.
Note: These cart commands require an authenticated user and that the user actually owns the specified carts (or is able to impersonate the owner).

Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Morten,
This was actually the first hack I had in mind :)

And yes, the users will be authenticated and own the carts.

Thank you very much for the help.

Adrian

 

You must be logged in to post in the forum