Developer forum

Forum » Development » eCom Cart Orderline Duplicates

eCom Cart Orderline Duplicates

Jeppe Jakobsen
Reply

Hello everyone

I have an issue when using the EcomCartLineAddedObserver. I've added two fields in the admin panel. The first is a Orderfield which contains a comma-separated list of single-letter values, example: (A,U). This list represents a select in the front-end, where the choice is stored in a Orderlinefield. The product is added using these parameters: "productid=<id>&cartcmd=add&EcomOrderLineFieldInput_Binding=U&Quantity=1" and so far everything is fine.

The issue then surfaces for some of our "special" users, that should not have option 'A'. This works fine in the front-end, and i've even implemented a EcomCartLineAddedObserver that validates the values set in EcomOrderLineFieldInput_Binding. The logic will set the value to 'U', if the submitted is deemed incorrect, but this is where a orderline duplication can happen.

Lets say a "special" user, that is abit more clever than the rest, has added a product with the above parameters, where "EcomOrderLineFieldInput_Binding=U". The user then manually submits a new product with "EcomOrderLineFieldInput_Binding=A". The logic i implemented then kicks in for the second product and changes the OrderLineFieldValue to 'U'. It does this (simplified) by getting:

bindingValue = orderLine.OrderLineFieldValues.First(x => x.OrderLineFieldSystemName == "Binding");

bindingValue.Value = "U";

When the user then navigates to his/her cart, there is TWO identical orderlines. I've noticed the eCom cart can automaticly merge orderlines when editing the binding on the orderline through the Cart, but this does not happen if the two orderlines become identical, due to some logic in a EcomCartLineAddedObserver. I imagine the validation of the cart runs before NotificationSubscribers.

I cannot find any observer that can be used for validation of the product before a orderline is created, so should i implement some manual logic for handling this type of case, using orderLine.CanBeMerged(line), or do you have another suggestion?

Best Regards, Jeppe.


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Jeppe

That is an ellaborate explanation, thank you for that. Problem understood.

Unfortunately I do not have a solution to this. If you just want to make sure that the user will not fraud the process, you can move the code from you subscriber to a much earlier subscriber, i.e. Page.Loaded which runs much earlier. That one has access to the cart as well using the Common.Context.Cart - so you can check against that at this time. Of course you cannot change what is going into the new orderline, but you can make sure it does not get added.

You cannot change the data before it gets added to the cart and make sure the lines are merged.

Best bet would be to change the amount on the existing orderline and remove the wrong one when the issue is detected.

BR Nicolai

 

 
Jeppe Jakobsen
Reply

Would this be an accepted solution when used in a CartLineAddedObserver?

// Check for duplicates
if (added.Cart.OrderLines.Any())
{
    var removeDuplicate = false;
    // Find any duplicates
    foreach (var orderLine in added.Cart.OrderLines)
    {
        // CanBeMerged is dynamicweb orderline function.
        if (orderLine.CanBeMerged(added.AddedLine) && orderLine.ID != added.AddedLine.ID) 
        {
            // Mark the orderline for removal, as you should not modify the loop you are enumerating
            removeDuplicate = true;
            // Add the quantity
            orderLine.Quantity += added.AddedLine.Quantity;
        }
    }
    if (removeDuplicate)
    {
        added.Cart.OrderLines.Remove(added.AddedLine);
        added.AddedLine.Delete();
    }
}

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Yes, that should be ok.

You have to test that the cart gets saved and calculated after this step (I believe it is). Otherwise call order.save() after this operation.

BR Nicolai

Votes for this answer: 1

 

You must be logged in to post in the forum