Beau Posted June 6, 2009 Share Posted June 6, 2009 I've noticed that it's possible to have duplicate domain name registrations added to the shopping cart. Not good. So I threw together a Javascript to at least alert the customer that they have duplicate items in their cart. It doesn't keep the order from being submitted, but alerting the customer to the issue of duplicate items if they don't want them should cut down on errors in the first place. Note: This code uses the YUI Javascript library. You can download the YUI file from YAHOO and install it within your site's JS folder or just use it from YAHOO's own repository. STEPS TO USE 1) Copy the below code into your viewcart.tpl {literal} <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js" ></script> <script> YAHOO.util.Event.onDOMReady(function(){ var items = YAHOO.util.Dom.getElementsByClassName('cartItem','td'); findDuplicates(items); }); function findDuplicates(cartItems){ if (cartItems.length > 1){ var itemCount = []; var hash; for (items in cartItems){ hash = cartItems[items].innerHTML.replace(/[^0-9a-zA-Z]+/g,"").toString(); if (isNaN(itemCount[hash])) { itemCount[hash]=1; } else { itemCount[hash]++; } } for (var items in itemCount){ if (itemCount[items] > 1) { alert('NOTICE! Your cart has duplicate item(s).\nPlease remove any items that may be duplicates.'); } } } } </script> {/literal} 2) Add the "cartItem" class to each of the the Description TD tags <td class="someclass cartItem"> The "cartItem" class acts as a kind of ID tag for the JS to grab and then account for duplicated item descriptions within the table. NOTE: There are a couple of different tables in the viewcart.tpl so make sure you add the class to each of them. -Beau 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.