Sunday, 8 September 2013

Sending information to litecommerce shopping cart using $.get

Sending information to litecommerce shopping cart using $.get

I'm building a site for my moms small business, and I'm using LiteCommerce
as a way for people to checkout and buy items; however I have my own
separate page acting as the storefront. Right now as you click on items
their sku numbers get saved in a temporary cart on my page. Then when the
"checkout" button is clicked I have code iterate through the sku numbers
and send $.get requests to LiteCommerce to populate its shopping cart.
Then, when the amount of requests sent is equal to the number of items
that were selected I have the page redirect to the LiteCommerce checkout
page. Everything seems to work fine; however, some items don't get
populated into the LiteCommerce shopping carts. If I send 10 items,
sometimes all 10 might go through, and sometimes a couple might be
missing.
This is the code for the checkout button I'm using:
//Click function for Checkout button.
$(document).ready(function(){
$("#checkout").click(function(){
var items = 0;
var getRequests = 0;
//Iterate through sku numbers
$(".item_sku").each(function(){
items++;
//Send "add to bag" ajax requests for LC
$.get("../litecommerce/cart.php?target=cart&action=add&product_id="
+ $(this).html())
.done(function(){
getRequests++;
if(items == 0){
//Do nothing
}
//Once all item request are fininshed, redirect to checkout
else if(getRequests == items){
window.location.href =
"../litecommerce/cart.php?target=checkout";
}
});
});
});
});
I've thrown alerts in several spots seeing if the correct sku numbers are
going through, and to see if the redirect is happening before all the
requests are finished. But everything seems to be working, yet when the
checkout page comes up theres usually 1 or 2 items that didn't go through.
I'm not sure if its something I'm doing wrong in my code, or maybe if it
has something to do with the LiteCommerce side not receiving the requests
properly.

No comments:

Post a Comment