2

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>
nikos83
  • 355
  • 1
  • 5
  • 19

1 Answers1

-1

You can serialize the data from product form and send it to server via AJAX request ( via Prototype | via jQuery | via plain JS).

Community
  • 1
  • 1
Neodan
  • 5,154
  • 2
  • 27
  • 38
  • Hi thanks for replay, I'm not sure how to do it, I can get link to the product using onclick="setLocation('getAddToCartUrl($_product) ?>')"> – nikos83 Nov 22 '15 at 10:08
  • All necessary data you can get from the product form. For example `var productForm = jQuery('#product_addtocart_form'); var requestURL = productForm.attr('action'); var requestPostData = productForm.serialize();` – Neodan Nov 22 '15 at 14:35
  • Hi I've updated script add to cart in my question, there's `serialize()` there, should I add variable in script to get this url for related product? Or get the product link via php and use in `productAddToCartForm.submit = function(button, url)` but how to? how to pass this url? Or Should I use `var requestURL = productForm.attr('action');` and in `productAddToCartForm.submit(requestURL)` ?? – nikos83 Nov 28 '15 at 18:14
  • Did you try just use `productAddToCartForm.submit()` (without parameters)? – Neodan Dec 11 '15 at 07:11