I've installed magento 2 fresh on localhost
, and trying to add items to cart but didn't work..
when click on add to cart button, button will change into adding and stays there, didn't add to cart item.
Any suggestion ?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityChange your base URL from localhost
to 127.0.0.1
.
Using localhost
as the domain in your base URL causes problems in Magento 2.
Magento's base URL configuration is located in the core_config_data
table. The following value
s may need to be changed:
web/unsecure/base_url
web/secure/base_url
For example, if you previously had http://localhost/magento2community/
, you should change it to http://127.0.0.1/magento2community/
After changing the url, flush the cache (bin/magento cache:flush
). You might also need to clear out your var directories.
localhost
to 127.0.0.1
,core_config_data
tableThose who don't wish to change the localhost url for this problem.
The problem is due to form_key mismatch. The form key which gets posted during add to cart action is getting mismatched with store session form key.
Go to
vendor/magento/module-checkout/Controller/Cart/Add.php execute()
method and comment out the below If condition.
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
NOTE: This is not a recommended solution for Production environment.
Reference: quick fix.
After my test, Magento 2 cannot work properly in single domain name, like http://localhost/
, http://mysite/
, http://localsite/
etc.
Other than use the 127.0.0.1 directly, we could change our site url like http://mysite/
to http://mysite.local/
, and add 127.0.0.1 mysite.local
to our computer host file, then we could use the site function like "add to cart" properly.
I had the same issue on my localhost. Basically what I did was change the base url from the admin area.
Go to store ->configuration ->web ->Base URL ->
change it to http://127.0.0.1/your_magento_installation/
it worked from me.
Issue can be solved by changing domain url from localhost to 127.0.0.1
Can be changed in database in core_config_data
set web/unsecure/base_url to http://127.0.0.1
After this clear magento cache and browser cache and its works.