How to redirect Add to Cart button in opencart to the cart page directly

In this tutorial, we will show you how to redirect “Add to cart” button in Opencart version 1.4+, version 1.5+, version 2.0+ and version 3.0+ and take you directly to the shopping cart page.

For Opencart 1.4+ versions

For Opencart 1.4 versions it’s easy we can do it through the admin section
In Extensions->Modules under “Cart” you can disable ajax

For Opencart 1.5+ versions

But in Opencart 1.5+ there are no options to do those things so we have to edit in the codes.

Open the javascript file at catalog/view/javascript/common.js

Find the code below :

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

Replace with

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

For OpenCart 2.3.0.2+ and Opencart 3.0+ follow following steps:

Find following code at catalog\view\javascript\common.js

var cart = { 'add': function(product_id, quantity) {

Inside that find success method:

success: function(json) {    $('.alert, .text-danger').remove();

Just below it add following code:

json['redirect']='index.php?route=checkout/cart';

Now open another file catalog\view\theme\ACTIVETHEME\template\product\product.tpl

Find following code:

<script type="text/javascript"><!-- $('#button-cart').on('click', function() {

Inside that find success method:

if (json['success']) {

Just below it add following code:

window.location.href='index.php?route=checkout/cart';

All is set now when someone add to cart then it directly redirect to cart page

Previous articleFree Automated Testing and monitoring of Opencart functionalities and sites
Next articleUSD to bitcoin, bitcoin exchange rate – Php code
Author of three Opencart book. The recent Opencart 4 book is at https://amzn.to/4dOlbOR

9 COMMENTS

  1. Hello,
    I am facing a problem. I need help. Actually In my template Success Alert Box default set.

    My question is when I click on add to cart button that time success alert box appear but there is an option to close icon

    But I need that Success box will hide automatically . So how to solve this ? anybody can help

    Thanks and help me out.

  2. The topics you chose are excellent choices, and your writing is excellent as well. I appreciate your sharing. I think all of your suggestions are fantastic! Well done!

LEAVE A REPLY

Please enter your comment!
Please enter your name here