Latest Articles
So far this was never an issue, there were problems with uploading modules via the installer because of the ftp connection but you could allways install ocmods without any issue. However, some .. Read More
People had this issue so many times I thought I would write a bit about it. The  types of issues with opencart's 2.x modification system that I identified so far are: 1. Old Vqmod .. Read More
This small tutorial will show you how to manually add custom links to the default top menu in opencart. If you dont want to go trough all the hustle and would like to be able to completly customize.. Read More
To enable seo urls in opencart navigate in admin to system > settings , edit the store and on the server tab of the store settings you will find "Use SEO urls", set it to yes. So far e.. Read More
All Articles

Add to cart not working on opencart homepage

A very common problem that many users encounter is the Add to Cart sometimes not working on their site homepage.

The problem is related to the url of the store. A website can be accessed trough adress like: example.com as well as www.example.com

When you first install opencart, the base of the website is going to be written in config.php, but it can only be one: either example.com or www.example.com (depends on the adress you accessed the installation in the first place). So if you install your store by going to example.com, then the base of your website in config.php will be example.com. So here is the origin of the problem, because if you then access your store via www.example.com , the add to cart wont work on the homepage.

To fix this problem, you can make a redirect in htaccess from www.example.com to example.com or the other way around (depends on which case applies to you).

First, to make a redirect from non www to  www adress (ex: from example.com to www.example.com), edit the .htaccess from your opencart root and find the line:

 

RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

 

and below it add the code:

 

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

 

To make a redirect from www to non www adress (ex: from www.example.com to example.com), edit the same file mentioned above, and below the same line mentioned, add the code:

 

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

 

In both cases, replace example.com with the adress of your store.

Posted by Flo

Related Articles

People had this issue so many times I thought I would write a bit about it. The  types of issues with opencart's 2.x modification system that I identified so far are: 1. Old Vqmod version installed which doesnt support ocmod 2. Ocmod cache could not be written due to insufficient folder permission 3. The ocmod cache doesnt get loaded in frontend (ocmod works in admin but not in frontend) The most common I encountered is number 3 because it has to do with the opecart installation method. Some control panel software installers do not properly configure the ope..
0 comments on this article - view comments

30 Comments To "Add to cart not working on opencart homepage"

Rachel On 01 Dec 2016
Many thanks! It is of great help to me. Reply to this comment
mike On 17 Oct 2016
Thanks for your post. It's really helpful. Can i share this on my webiste: http://www.zetarmold.com Thanks Reply to this comment
Flo On 17 Oct 2016
Hello, Yes you can do that.
krishna On 04 Dec 2015
My problem was same i just changed the jquery path fromlocal to cdn in file : catalog/view/theme/default/template/common/header.tpl. <!--<script async src="catalog/view/javascript/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> Reply to this comment
Sohoz On 05 Jun 2015
I've changed all code and redirect successfully but problem is after 301 redirect non sohozlife.com to www.sohozlife.com 'Add to Cart' not working (Opencart). Please suggest me what i've to do Reply to this comment
omego On 16 Nov 2014
Many thanks, amazing quick fix. Reply to this comment
Bruce On 27 Oct 2014
Thank you. Just reinstalled my shop using Opencart 2 and a customer reported inability to add to cart. I could replicate the issue using www in front of my domain. The .htaccess mod you've stated fixed it for me (apparently not for the customer - but I put that down to cookies and browser issues). Thanks again Reply to this comment
David On 21 Jun 2014
Solved the problem by adding this: RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE] Just after this: RewriteBase /shop/ Reply to this comment
Davie On 19 Jun 2014
What if my Opencart installation is in a subfolder like http://www.domainname.com/webshop and I want to redirect to http://domainname.com/webshop How would the .htaccess code look like then? Reply to this comment
Richard On 22 Apr 2014
its very simple, just go to you cpanel and go to redirect. set it to redirect like example.com --> www.example.com or the other way around to fit what is in your config.php Reply to this comment
sarika On 15 Jan 2014
I have same problem . i installed my store without http://domainname.com but later on i added www via .htaccess ( by using redirection) http://www.domainname.com . now my addtocart is not working. what should i do ? should i change config.php too ? from "http://domainname.com" to "http://www.domainname.com" ? Please reply asap . :( Thanks! Reply to this comment

Write a comment

Your Name:


Enter the code in the box below:

Your Comment:
Note: HTML is not translated!