1

I want to have 2 buttons on the product view page one will just add product to cart and it will not redirect it to cart. However other will add product to cart and also redirect the user to cart. So please guide me how this can be achieved. PLease refer the image to get clear idea what I mean to say

I am adding my code here please check

<button type="button" class="button" onClick="addcart(this.id);productAddToCartForm.submit(this);redirectcart();"  id="buyitnowid">
             <span style="background:green;"><i class="icon icon-shopping-cart"></i><?php echo $this->__('BUYNOW'); ?></span>
        </button>

Java script is

productAddToCartForm.submit = function(button, url) {
        if (this.validator.validate() &&  !addOptions.submittedForm) {
            var form = this.form;
            var url = form.action;

            if (weltpixel.quickview.inIframe) {
                parent.weltpixel.quickview.addToCart(url, $(form).serialize(true), addOptions);
            } else {
                weltpixel.quickview.addToCart(url, $(form).serialize(true), addOptions);
            }

            addOptions.submittedForm = true;
        } else {
            jQuery('html,body').animate({
                scrollTop: jQuery("#product-options-wrapper").offset().top -100
            });
        }
    }.bind(productAddToCartForm);
  • Is your "Add to cart" button working properly via ajax ? – Prateek Jan 19 '16 at 11:48
  • 1
    Yes it is working – Pratik bhatt Jan 19 '16 at 12:10
3

You can achieve this via param return_url. On click of "Add to cart", fire your ajax call with return_url set as current product page URL. When clicking on "Buy Now", set the param to cart page URL. Magento will handle the rest. You'll need to have some JS in place for both of the buttons.

Edit :

Below is the code I could gather. In addtocart.phtml file, your buttons will go like this :

<button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" data-url="<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
<button type="button" title="<?php echo $buttonTitle ?>" id="product-buynow-button" data-url="<?php echo $this->getUrl('checkout/cart'); ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $this->__('Buy Now'); ?></span></span></button>

Now, in the form in view.phtml file, add a hidden field like this :

<input type="hidden" name="return_url" id="return_url" value="" />

In that file, you can see the JS in action to submit the form. Only a single line modification needed now here. Final JS will be like :

productAddToCartForm.submit = function(button, url) {
    if (this.validator.validate()) {
        jQuery('#return_url').val(jQuery(button).attr('data-url'));
        var form = this.form;
        var oldUrl = form.action;

        if (url) {
            form.action = url;
        }
        var e = null;
        try {
            this.form.submit();
        } catch (e) {
        }
        this.form.action = oldUrl;
        if (e) {
            throw e;
        }

        if (button && button != 'undefined') {
            button.disabled = true;
        }
    }
}.bind(productAddToCartForm);

Here, I have just set the input field value before sending the data to controller.

  • If you perform an AJAX call there will be no redirection of the main page so I don't understand your point and the need of JS. – Alessandro Ronchi Jan 19 '16 at 6:54
  • @AlessandroRonchi, You are right, main page won't be re-directed, but the call you made will have response HTML from the page you specify in that param. Based on that response, you can decide what to do next in JS itself. This way, there is no need to have any custom module controller, default will be good to go. – Prateek Jan 19 '16 at 6:59
  • I think he can simply use the additional redirect_url to force redirection as he desires. The Mage_Checkout_CartController::_goBack() method should handle it. – Alessandro Ronchi Jan 19 '16 at 7:07
  • added code please help me I am not too good in AJAX... – Pratik bhatt Jan 19 '16 at 10:41
  • 1
    @Pratikbhatt, You are using some custom plugin right now to add to cart via Ajax. I'm not aware of this plugin and its functionality. I'll look around for some code that I might have for this but you'll need to modify that to suite your needs. – Prateek Jan 19 '16 at 11:47
0

As Prateek suggests, you can use the redirect_url parameter to force redirection after the product has been added to cart.

Refer to Mage_Checkout_CartController::addAction() to see what happens when you add your product to cart and in particular to Mage_Checkout_CartController::_goBack() to see how redirection works.

  • added code please help me I am not too good in AJAX... – Pratik bhatt Jan 19 '16 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.