Developer forum

Forum » Templates » Add to cart multiple times the same product at once

Add to cart multiple times the same product at once

Gaetan Di Caro
Reply

Hello,

I have a product which has an orderlinefield for selecting a length. I want the customer to be able to select several quantities of several length and add them all to cart at once, but I'm struggling. It works when I only select a quantity and a length for only one line, but as soon as I select 2 or more, I get the attached error message : "An item with the same key has already been added."

Here is my rendered HTML :

<form method="post" id="multiForm" name="multiForm">
    <input type="hidden" value="addMulti" id="CartCmd" name="CartCmd">

        <div>
            <input type="hidden" value="1" id="ProductLoopCounter1" name="ProductLoopCounter1">
            <input type="hidden" value="806" id="ProductID1" name="ProductID1">
            <input type="hidden" value="" id="VariantID1" name="VariantID1">
            <input type="hidden" value="" id="UnitID1" name="UnitID1">
            <input type="hidden" value="0" id="wishListID1" name="wishListID1">

            <input type="number" value="0" id="Quantity1" name="Quantity1">
            <select id="EcomOrderLineFieldInput_SelectedLength1" name="EcomOrderLineFieldInput_SelectedLength1">
                    <!--options...-->
            </select>
        </div>
        
        <div>
            <input type="hidden" value="2" id="ProductLoopCounter2" name="ProductLoopCounter2">
            <input type="hidden" value="806" id="ProductID2" name="ProductID2">
            <input type="hidden" value="" id="VariantID2" name="VariantID2">
            <input type="hidden" value="" id="UnitID2" name="UnitID2">
            <input type="hidden" value="0" id="wishListID2" name="wishListID2">

            <input type="number" value="0" id="Quantity2" name="Quantity2">
            <select id="EcomOrderLineFieldInput_SelectedLength2" name="EcomOrderLineFieldInput_SelectedLength2">
                    <!--options...-->
            </select>
        </div>
        
        <div class="addtocart-bylength " id="addtocart-bylength-3">
            <input type="hidden" value="3" id="ProductLoopCounter3" name="ProductLoopCounter3">
            <input type="hidden" value="806" id="ProductID3" name="ProductID3">
            <input type="hidden" value="" id="VariantID3" name="VariantID3">
            <input type="hidden" value="" id="UnitID3" name="UnitID3">
            <input type="hidden" value="0" id="wishListID3" name="wishListID3">

            <input type="number" value="0" id="Quantity3" name="Quantity3">
            <select id="EcomOrderLineFieldInput_SelectedLength3" name="EcomOrderLineFieldInput_SelectedLength3">
                    <!--options...-->
            </select>
        </div>
    
    <button type="submit" name="submit">Add to cart</button>
</form>

 I can't put my finger on what's wrong here. Any idea ?

Thanks !

2016-03-03_09_37_03-Photos.png

Replies

 
Nicolai Høeg Pedersen
Reply

You are adding the same productid twice on the same post. That is not possible. DW gets confused when you do that!

 
Gaetan Di Caro
Reply

Ah, that's what I was afraid of. Is there a simple way of doing what I want ?

Right now I'm thinking of calling the add to cart url for each line on clic of the button via javascript.

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

Post one form at a time using ajax?

Votes for this answer: 1
 
Gaetan Di Caro
Reply

That's what I ended up doing. Works fine like that, thanks.

Could be interesting in the future though to be able to do it the way I did on my first post, easier to handle and consistent with the way of doing it for multiple different products..

 
Nicolai Høeg Pedersen
Reply

I had a developer look into it, and it is happening because of the way Dynamicweb adds quantity discounts.

We will make a minor change to this so it will be possible in a later release.

Thanks, Nicolai

 
Gaetan Di Caro
Reply

Actually I still have a problem. I can add to the basket alright, but after some time (minutes ?), the error that I attached randomly appears and doesn't go away unless I empty my cookies. Seems like at some point in the session the basket is recalculated and having the same product several times causes the error you just described. Therefore I'll be quite waiting for that fix. Should I open a bug case ?

 
Nicolai Høeg Pedersen
Reply

Yes, I'll have a bug fix done that handles the issue with discounts for the same product id on multiple orderlines.

 
Gaetan Di Caro
Reply

Hi Nicolai,

Do you have any visibility on that fix ? We are going to need that feature quite soon.

Thanks !

 
Nicolai Høeg Pedersen
Reply

It was changed with TFS#21869 which is scheduled for 8.8.1 due April 26th.

BR Nicolai

 
Gaetan Di Caro
Reply

Alright, thanks. However, can I ask for this to be escalated ? Our project is scheduled to go live before that and this problem is blocking any release. Thank you !

 
Nicolai Høeg Pedersen
Reply

I've asked for a merge to 8.8 branch, will be released with the next hotfix.

 

You must be logged in to post in the forum