- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Adding An Add To Cart Link
Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
Hello,
I am attempting to add an "Add To Cart" link for products below their 1-5 star rating on thumbs pages.
I think the best approach might be to use the .insertAfter() technique for the class name of the ratings div, but was unsure. Would something like this work?:
<div class="addToCartLink"><a href="/checkout/cart">Add To Cart</a></div> $('.addToCartLink').insertAfter('.rating-container')
Any and all feedback here is greatly appreciated.
Thank You
Re: Adding An Add To Cart Link
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
hi @KO,
The idea is correct, but your syntax is not.
To insert a link, I'd go with something like the following:
var newLink = "<div class='addToCartLink'><a href='/checkout/cart'>Add To Cart</a></div>"; $(newLink).insertAfter('.rating-container');
You will need to make sure that the link you add actually performs an add to cart. The example above is merely a link to the the cart page.
Analytics and Testing Guru




Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
Hi There,
Thanks for responding. I was actually able to get it done this way:
$(".rating-container").after("<div id=\"optimizely_795437338\"><strong>ADD TO CART</strong></div>"); $("<div id=\"optimizely_795437338\"><strong>Add To Cart</strong></div>").attr({"href":"/checkout/cart"}); /*injects a div after the ratings container "ADD TO CART"link clicks through to color/size selection on desktop this button clicks through to QuickView kept all caps to mirror desktop experience*/
Thanks for your help!
Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
While your code functions as expected, I'd recommend refactoring it like so:
var referenceElem = ['<div id="optimizely_795437338">', '<strong>ADD TO CART</strong>', '</div>'].join(''); $(".rating-container").after(referenceElem); $(referenceElem).attr({"href":"/checkout/cart"});
This is much cleaner, and easier to read for anyone else working on the code.


Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
what's the purpose of using the .join(''); at the end of the variable declaration?
Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report


Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report


Re: Adding An Add To Cart Link
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report
$("#yourelement").mousedown(function (){
window.open('yourlink.com', '_blank');
});