Page 1 of 2

When customer hits Add to Cart button, want to redirect them

Posted: Wed Nov 10, 2010 3:13 pm
by freni32
Right now when a customer looks at my product on my website, and then hits the "Add To Cart" button, it stays on the same page they were on but moves that product to the left sidebar and adds the product to the cart. My site most of the customers are older and not advanced on computers and think that the button "Add To Cart" isn't working. I would like for the system instead when they hit "Add To Cart" to take them to the "View Cart" page, where they can see the total and the price, and then hit "Checkout" from there. How would I do this?

Re: When customer hits Add to Cart button, want to redirect

Posted: Wed Nov 10, 2010 10:24 pm
by Qphoria
In Extensions->Modules under "Cart" you can disable ajax

Re: When customer hits Add to Cart button, want to redirect

Posted: Thu Nov 11, 2010 1:52 am
by freni32
Wow that was easy!!! thank you very much!!

Re: When customer hits Add to Cart button, want to redirect

Posted: Thu Jul 14, 2011 5:58 am
by oleg
In opencart 1.5.0.5v, there is no way to do it from the admin panel like in earlier versions.

I have seen different php based solutions that haven't worked for me, so I've came with my own javascript based solution.

The idea is very simple. You just have to append a redirect command to onclick event.

instead of

Code: Select all

<a class="button" onclick="addToCart('73');">
use

Code: Select all

<a class="button" onclick="addToCart('73');window.location='index.php?route=checkout/cart';">
or (not tested yet) if there are no onclick event, just add one with the redirect, or just add the redirect at the end of the addToCart function

Re: When customer hits Add to Cart button, want to redirect

Posted: Thu Jul 14, 2011 9:54 pm
by oleg
There is a little problem with this method. Sometimes the redirect works faster than the ajax add to cart code, it does add a product to cart, but when you get to the carts page you don't see the product until you refresh. To solve this you can use setTimeout().

add to common.js function

Code: Select all

function RedirectToCart(){
	window.location='index.php?route=checkout/cart';
}
and instead of

Code: Select all

<a onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button">
use

Code: Select all

<a onclick="addToCart('<?php echo $product['product_id']; ?>');setTimeout('RedirectToCart()',2000);" class="button">

Re: When customer hits Add to Cart button, want to redirect

Posted: Fri Jul 22, 2011 12:47 am
by Emoney
The problem is that, regardless of whether the AJAX module is enabled and turned on, there is still a line of code in your theme's common/header.tpl that includes an AJAX call on click.

So the simple solution is to do the window.location trick to redirect -- after the AJAX call has returned successfully, to avoid asynchronous issues and javascript timeout hacks.

Here's my modified code from my header.tpl file. I hope this helps.

<script type="text/javascript">
$(document).ready(function () {
$('#add_to_cart').removeAttr('onclick');

$('#add_to_cart').click(function () {
$.ajax({
type: 'post',
url: 'index.php?route=module/cart/minicart',
dataType: 'html',
data: $('#product :input'),
success: function (html) {
$('#header_cart .center').html(html);
window.location='index.php?route=checkout/cart'; #redirect to cart
}
});
});
});
</script>

Re: When customer hits Add to Cart button, want to redirect

Posted: Thu Jul 28, 2011 6:36 am
by T3CustomPCs
Hi all i want to do is when customers add to cart it take them to the shopping cart page.
I am using OC 1.5.1.1 with the Shoppica theme.
Could you please tell me exactly what file i need to edit and what exactly i need to find and replace.

Re: When customer hits Add to Cart button, want to redirect

Posted: Wed Aug 03, 2011 4:53 pm
by uksitebuilder
in catalog/view/javascript/common.js

find:

Code: Select all

			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
replace with:

Code: Select all

			if (json['success']) {
				window.location='index.php?route=checkout/cart'; 
			}	

Re: When customer hits Add to Cart button, want to redirect

Posted: Mon Aug 08, 2011 10:46 pm
by let-it-go
@uksitebuilder, thanks, works well for my site. And i also did it in the product.tpl.

Re: When customer hits Add to Cart button, want to redirect

Posted: Fri Aug 12, 2011 4:47 pm
by darrengould
Did it work because I just tried it and couldn't get it to work for me

Using V1.5.1

Still don't know why showing the cart isn't an option as I prefer it that way and always will.

At least some people expect to see what they have ordered when they click on a button to add to cart.

If others prefer it this way fine but make it a choice thing. Just another bit of OC that appears to be faltering.

I seriously would have left OC ages ago if I hadn't spent so much on mods.

Re: When customer hits Add to Cart button, want to redirect

Posted: Fri Aug 12, 2011 8:52 pm
by tunnu
I am using 1.4.9.4

its not working when i turn off ajax in Extensions>Cart

Help me now :(

Re: When customer hits Add to Cart button, want to redirect

Posted: Sat Oct 08, 2011 6:51 am
by kommancero
Hi I'm using 1.5.3 and I want to put an if statement after user clicks add to cart button. I could not find where to put that if statement. can anyone help?

Re: When customer hits Add to Cart button, want to redirect

Posted: Sat Oct 08, 2011 3:16 pm
by uksitebuilder
What operation will the if statement perform ?

Re: When customer hits Add to Cart button, want to redirect

Posted: Tue Oct 11, 2011 10:56 pm
by kommancero
I'm going to check a manufacturer's id from database. and compare it with the one's that is already in the cart. So basically I want the customers to buy from only one manufacturer.

Re: When customer hits Add to Cart button, want to redirect

Posted: Sat Oct 29, 2011 1:08 am
by sferris
uksitebuilder wrote:in catalog/view/javascript/common.js

find:

Code: Select all

			if (json['success']) {
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.attention').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
replace with:

Code: Select all

			if (json['success']) {
				window.location='index.php?route=checkout/cart'; 
			}	

I'm using 1.5.0.5 and this works beautifully on the home and categories page, but not the products page. Here's the weird thing - it works if I click add to cart on a product in the Related Products tab, but it doesn't work on the button right next to the product on the page (doesn't matter if there are related products or not).

Any ideas?

Re: When customer hits Add to Cart button, want to redirect

Posted: Sat Oct 29, 2011 1:23 am
by sferris
Answered my own question after I looked at the code a little closer. On the products.tpl, the button code is:

<a id="button-cart" class="button"><span><?php echo $button_cart; ?></span></a>

and it should be:

<a class="button" onclick="addToCart('<?php echo $product_id; ?>');"><span><?php echo $button_cart; ?></span></a>

Once I change this, it worked. Just an FYI for anyone else having issues.

Re: When customer hits Add to Cart button, want to redirect

Posted: Sat Oct 29, 2011 1:31 am
by uksitebuilder
Add to cart javascript for the products page is at the bottom of the product.tpl rather than in the common.js file

Re: When customer hits Add to Cart button, want to redirect

Posted: Wed Feb 15, 2012 7:55 am
by Gui Siani
iksitebuilder, Where exactly do we have change in product.tpl?

Re: When customer hits Add to Cart button, want to redirect

Posted: Sun Feb 19, 2012 3:11 am
by jparkley
Thanks, sferris! I'm using 1.5.1.3 and it works for me!

Re: When customer hits Add to Cart button, want to redirect

Posted: Fri Feb 24, 2012 1:05 am
by rasahydro
I did this and found that it does not work on internet explorer but it does work on mozilla and safari