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 ?

6 Answers 6


Change your base URL from localhost to

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 values 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

After changing the url, flush the cache (bin/magento cache:flush). You might also need to clear out your var directories.

  • 1
    why is the answer written as code?
    – Jon
    Commented Dec 21, 2016 at 13:35
  • 1
    This solved my problem on MAMP. Commented Jan 24, 2018 at 9:37
  • 1
    Working fine thanks. Commented Oct 18, 2019 at 6:07
  1. Please change localhost to,
  2. To change directly from database Goto -> core_config_data table
  3. Then change secure and unsecure base url

http://localhost/ to


Those 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 directly, we could change our site url like http://mysite/ to http://mysite.local/, and add mysite.local to our computer host file, then we could use the site function like "add to cart" properly.

  • Yes, that work with .local append at last, but how this work?
    – Sourav
    Commented Jul 13, 2020 at 7:39
  • @Sourav The official codes don't consider using single domain name as site url, because it is impossible in production env.
    – Key Shang
    Commented Jul 13, 2020 at 8:41

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

it worked from me.


Issue can be solved by changing domain url from localhost to

Can be changed in database in core_config_data

set web/unsecure/base_url to

After this clear magento cache and browser cache and its works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.