Jump to content

Duplicate Items Alert for View Cart


Beau

Recommended Posts

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated