Jquery & Additemtocart Ajax Minicart: Servlet
Jquery & Additemtocart Ajax Minicart: Servlet
minicart
I know a lot of stuff in ATG uses Dojo for javascript, but I have had good luck with
JQuery as well.
frags/header.jsp
<%@ taglib uri="dsp" prefix="dsp"%>
<dsp:page>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<%-- accepts param from include for title, keywords, and description --
%>
<title><dsp:valueof param="pageTitle">petsovernight.com - any pet,
anywhere, overnight!</dsp:valueof></title>
<meta name="keywords" content="<dsp:valueof param="metaKeywords" />" />
<meta name="description" content="<dsp:valueof
param="metaDescription" />" />
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-
1" />
<script src="scripts/jquery-1.2.3.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
function addtocart() {
var selObject = returnObjById("selectSKU");
var selIndex = selObject.selectedIndex;
var selQuantity = returnObjById("quantity");
if(selObject.value == "") {
alert("please select a size");
return false;
}
if(selQuantity.value == "") {
alert("please select a quantity");
return false;
}
updateCartURL = "frags/minicart.jsp?
dcs_action=additemtocart&url_catalog_ref_id=" +
selObject.options[selIndex].value + "&url_product_id=<dsp:valueof
param="productId"/>&url_quantity=" + selQuantity.value;
cartsummaryURL = "frags/cartsummary.jsp";
$('#minicart').load(updateCartURL,function(){$
('#cartsummary').load(cartsummaryURL);});
$('#minicart').slideDown('slow');
}
function returnObjById(id) {
if (document.getElementById) {
var returnVar = document.getElementById(id);
} else {
if (document.all) {
var returnVar = document.all[id];
} else {
if (document.layers) {
var returnVar = document.layers[id];
}
}
}
return returnVar;
}
</script>
<div id="minicart" style="width: 300px;z-index: 150;position:
absolute;top: 30px;left: 200px;color: #fff;display: none;">
.
</div>
...
frags/minicart.jsp
<%@ taglib uri="dsp" prefix="dsp"%>
<%@ taglib uri="c" prefix="c"%>
<dsp:page>
<dsp:importbean
bean="/atg/commerce/order/formhandler/ShoppingCartFormHandler" />
<dsp:importbean bean="/atg/commerce/ShoppingCart" />
<dsp:importbean
bean="/atg/commerce/order/purchase/RepriceOrderDroplet" />
<dsp:importbean bean="/atg/dynamo/droplet/ForEach" />
<dsp:importbean bean="/atg/dynamo/droplet/Switch" />
<dsp:importbean bean="/atg/dynamo/droplet/IsNull" />
<dsp:droplet name="RepriceOrderDroplet">
<dsp:param value="ORDER_SUBTOTAL" name="pricingOp" />
</dsp:droplet>
<script type="text/javascript">
$('a#continueshop').click(function() {
$('#minicart').slideUp(400);
return false;
});
</script>
</div>
</dsp:page>
For this example, you have to have a select with the sku in it (called "selectSku"), a select
with the quantity (called "quantity") and a javascript submit button that calls
addItemToCart.
product.jsp
<%@ taglib uri="dsp" prefix="dsp"%>
<dsp:page>
<dsp:importbean bean="/atg/commerce/catalog/ProductLookup" />
<dsp:importbean bean="/atg/commerce/catalog/CategoryLookup" />
<dsp:importbean bean="/atg/commerce/catalog/ProductLookup" />
<dsp:importbean bean="/atg/commerce/pricing/PriceItem" />
<dsp:importbean bean="/atg/dynamo/droplet/Switch" />
<dsp:importbean bean="/atg/targeting/TargetingForEach" />
<dsp:importbean bean="/atg/commerce/catalog/ProductBrowsed" />
<dsp:importbean bean="/atg/dynamo/droplet/IsNull" />
jQuery makes it really easy to fill divs with the content of external pages. It also has some
nice functions for transitions.
This allows us to treat the page fragments as individual objects to some extent.
The AddITemToCartServlet is mapped in the pipeline to look for any page request with
specific parameters. parameters are actually caught and forwarded to it by the
CommerceCommandServlet. For more information, look up "Adding an Item to an Order
via a URL" in the ATGCommerceProgrammingGuide.
The Servlet takes the following params that we are interested in:
There are other params that this servlet takes as well - you could even create a form to
update/edit items in the cart.
So, basically, the form sets the sku and the quantity, and the javascript on the button calls
addtocart() javascript function.
The addtocart() function inspects the form values, creates the URL to call (the minicart
page, plus the URL paramaters to invoke the AddITemToCartServlet). It then fills a
hidden div with the content of the ajax response, and slides the cart open for the user to
see.