18

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 ?

29

Change 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 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 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.

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

http://localhost/ to http://127.0.0.1

6

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.

2

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.

1

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.

0

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.

protected by Community Jun 5 '17 at 3:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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