2

So right now I have a scenario where I want 4 fields to add some products to the cart (these are called Device ID, each one represents one product that needs to go to the cart), so here's the code where I added the add_to_cart_form:

<div>
                <div class="device">
                    {exp:cartthrob:add_to_cart_form entry_id="device1" price="9.95" shipping_exempt="yes"}
                        <p class="device-id ">Device ID</p>
                        <input class="deviceInput" type="text" name="device1" placeholder="Device Id" />
                    {/exp:cartthrob:add_to_cart_form}
                </div>
                <div class="device">
                    {exp:cartthrob:add_to_cart_form entry_id="device2" price="9.95" shipping_exempt="yes"}
                        <p class="device-id ">Device ID</p>
                        <input class="deviceInput" type="text" name="device2" placeholder="Device Id" />                    
                    {/exp:cartthrob:add_to_cart_form}
                </div>
            </div>
            <div>
                <div class="device">
                    {exp:cartthrob:add_to_cart_form entry_id="device3" price="9.95" shipping_exempt="yes"}              
                        <p class="device-id ">Device ID</p>
                        <input class="deviceInput" type="text" name="device3" placeholder="Device Id" />
                    {/exp:cartthrob:add_to_cart_form}
                </div>
                <div class="device">
                    {exp:cartthrob:add_to_cart_form entry_id="device4" price="9.95" shipping_exempt="yes"}
                        <p class="device-id ">Device ID</p>
                        <input class="deviceInput" type="text" name="device4" placeholder="Device Id" />
                    {/exp:cartthrob:add_to_cart_form}
                </div>
            </div>

Then, after that, there are two possible cases:

#1. The user wants to create a new account and add the items to the cart, in this case the items are added correctly and the user only needs to be redirected to the create account form.

#2. The user wants to log in to an existing account, and here's where I'm having problems. The problem is exactly when I'm not logged in and I want to log in, then the items are not added to the cart, the user logs in correctly and is redirected to the right page but the cart has no items on it.

This is the code I'm using to log in:

$(document).ready(function() { 
        $('form#login-form').ajaxForm(function(arg1, arg2, arg3){
            $("#login-error").hide();
            var response;
            try{
                response = JSON.parse(arg1);
            }catch(e){

            }
            if(response != undefined && response.success == false){
                $("#login-error").show();
                return false;
            }else{
                $("div.device div input[name=XID]").val(response.XID); //thing I tried to work with the add, just adds one item
                submitCartItems();
                window.location = "/purchase/cart"
            }
            return false;
        }); 
    }); 

And this is the code I'm using to submit the items to the cart:

function submitCartItems(){
        $("input.deviceInput").each(function(){
            if(!$(this).val())
                return;
            var form = $(this).closest("form"); 
            $(form).ajaxForm(cart_form_options);
            $(form).submit(); 
        });
    }

As you can see in a comment in the Login function, I used this to try to play a bit with the XID in the hidden inputs for each add_to_cart_form, the thing is it worked but just for one item, doesn't matter if I try to add all 4 items, only 1 gets added to the cart:

$("div.device div input[name=XID]").val(response.XID);

So the question is, is there a way to add the items to the cart via ajax request AFTER the login is done, or, is there a guest session/login session involved in the cart managing that's affecting the items being added to the cart?

  • So, on the same html page, you have a login form (assuming it is rendered by EE tags) and 4 add_to_cart_form elements. And you're trying to handle both by ajax. If that's the case, it sounds like the add_to_cart_form elements are being rendered at page load with guest session data (since you're not logged in yet). Then, you log in via ajax (no longer guest) and you submit the devices that are still pointing to a guest session. From there, I have actually no idea what to do, but I wanted to get some clarity. – Byron Sommardahl Jun 20 '14 at 14:01
0

What I would suggest you do to get that code to load into the template after login is successful, is do a $('div.add2cart').load('path to template with code'); then run your JS to add items to cart after the html loads with the new session information on a logged in user.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.