I'm trying to use ajax add to the cart in related products but I don't know how to get link to the each related products?
I would like to use ajax and not be redirected to the cart page when I click add to the cart. For the main product this is working well. But for related is adding the main product or adding related product but redirect me to the cart.
<?php endif; ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
</a>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
Can I use
productAddToCartForm.submit(this)
But what to pass inside submit?
This is the add to cart code
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
if (!url) {
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","ajaxcart/index"); // New Code
var data = jQuery('#product_addtocart_form').serialize();
data += '&isAjax=1';
jQuery('#loading-mask').show();
try {
jQuery.ajax({
url : url,
dataType : 'json',
type : 'post',
data : data,
success : function(data) {
jQuery('#loading-mask').hide();
if(data.status == 'ERROR'){
alert(data.message);
}else{
if(jQuery('.top-links .right-links')){
jQuery('.top-links .right-links').replaceWith(data.toplink);
}
jQuery('#after-loading-success-message').show();
}
}
});
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
}
}.bind(productAddToCartForm);
//]]>
</script>