1

I have added an "Add to Cart" button for the upsell items on the product view page, using the following code:

<button type="button" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_link) ?>')">
  <?php echo $this->__('Add to Cart') ?>
</button>

The problem is that when adding the product to the cart it then redirects to the home page. I have the After Adding a Product Redirect to Shopping Cart option set to No in the checkout configuration.

How do I get it to stay on the same page?

4

Magento have a function _goBack() at Mage_Checkout_CartController,which is give us the facility to redirect user at any page after successfully Cart from addAction by add an extra parameter( return_url) with add to cart url.

protected function _goBack()
    {
        $returnUrl = $this->getRequest()->getParam('return_url');
      /* Check have value of return_url  */
        if ($returnUrl) {

            if (!$this->_isUrlInternal($returnUrl)) {
                throw new Mage_Exception('External urls redirect to "' . $returnUrl . '" denied!');
            }

            $this->_getSession()->getMessages(true);
      /* then redirect to this page */
            $this->getResponse()->setRedirect($returnUrl);

At _goBack magento have check,current Action have any value for return_url param.If have then it redirect to customer to that page.

For this case,you need send a extra parameter return_url with add to cart url.

Currently,you getting cart url from $this->helper('checkout/cart')->getAddUrl($_link) and parent current page url as return_url value

<button type="button" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_link).'?return_url= '.Mage::helper('core/url')->getCurrentUrl() ?>')">
  <?php echo $this->__('Add to Cart') ?>
</button
  • Amit Bera@ Go admin side :System -> Checkout -> After Adding a Product Redirect to Shopping Cart option set to Yes then is working fine? – Abdul Jan 19 '16 at 6:54
  • Hey @Amit Bera. The issue was with my varnish configuration, it was adding the port 8080 to the return urls. Thank you showing me how that works, I never knew about return_url/_goback and I'm sure it will prove useful in the future. – iamlolz Jan 19 '16 at 6:58
3

When you add a product to the cart if 'redirect to shopping cart' is disable the controller action tries to:

  1. Find a "uenc" paramenter (is a hash with the url for redirect to)
  2. Get the referer url (from the request)
  3. If 1 and 2 fails then redirects to the home

Mage_Checkout_Helper_Cart::getAddUrl() accept an array as second parameter so when you are adding products outside the product view page, you can add the uenc parameter for the current url like that:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$uenc = Mage::helper('core/url')->urlEncode($currentUrl);
// Note that you can use any url you want.
... 
$this->helper('checkout/cart')->getAddUrl($_link, array('uenc'=> $uenc))
...

EDIT: Try this (Using the same concept as @AmitBera) but without concatenation:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();

... 
$this->helper('checkout/cart')->getAddUrl($_link, array('return_url'=> $currentUrl))
...
  • Hi @MauroNigrele. Thanks for the info mate, unfortunately this still redirects to the home page, I was adding them to cart from the product page view, not outside of it.(I've made that clearer in my original post) Thank you for going through the logic of how it works, that is really useful. – iamlolz Jan 19 '16 at 6:37
  • Hi @MaruoNigrele. The issue was with my varnish configuration, it was adding the port 8080 to the return urls. Thanks again for that info, it's good to know more about how that works. – iamlolz Jan 19 '16 at 6:57
  • cool! remember to always try new features with and without cache(s) specially varnish and fpc. – MauroNigrele Jan 19 '16 at 6:59
  • I have all the other cache's disabled but naively thought varnish wouldn't effect this. Thank you for your time mate. – iamlolz Jan 19 '16 at 7:05
1

just try this

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_link) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
  • Hi @Adbul. Unfortunately this also redirects to the home page after adding the product to the cart. – iamlolz Jan 19 '16 at 6:05
  • It is working fine. Pls clear magento Cache and try again – Abdul Jan 19 '16 at 6:39
  • Hi @Abdul. I don't have the cache enabled at all, that's not the issue. – iamlolz Jan 19 '16 at 6:42
  • function getAddToCartUrl(); is working base on admin side setting – Abdul Jan 19 '16 at 6:44
  • After Adding a Product Redirect to Shopping Cart option set to Yes then redirect cart page & No then redirect same page – Abdul Jan 19 '16 at 6:45
1

My particular issue was that I was using varnish and magento was adding port 8080 to the return url which produced an error as it seemed like an external url.

My quick fix was to add if ( $_SERVER['SERVER_PORT'] == 8080 ) { $_SERVER['SERVER_PORT'] = 80; } right before Mage::run($mageRunCode, $mageRunType); in index.php.

Not a permanent solution but it did the trick.

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.