If you regularly working with Magento 2 on Localhost environment on Windows server or Windows 10, you will probably see the error message “Your session has expired” when you click on add to cart button in Magento 2.3.
This is an annoying error because it won’t let you test any function related to shopping cart. Although this error was fixed in Magento 2.4, it still persists in Magento 2.3, so I decided to share some quick fixes for this error.
In this tutorial, I will explain the cause of the error as well as all possibles solutions on how to fix this error.
Cause of the error
The error happens because Windows can’t translate the localhost
URL to 127.0.0.1
in core_config_data
table, thus the add to cart function does not work properly.
Solution
As described above, the best solution for this issue is to set web/unsecure/base_url
and web/secure/base_url
to IP address 127.0.0.1.
We will make this change directly on the website’s database, so there’re 2 methods to do this:
Method 1: Update base_url using phpmyadmin.
Go to your phpmyadmin at localhost/phpmyadmin/ and click on SQL tab
Next, put these 2 commands in SQL tab
UPDATE `core_config_data` SET `value` = 'http://127.0.0.1/yourwebsite/' WHERE `core_config_data`.`path` = 'web/unsecure/base_url';
UPDATE `core_config_data` SET `value` = 'http://127.0.0.1/yourwebsite/' WHERE `core_config_data`.`path` = 'web/secure/base_url';
Replace http://127.0.0.1/yourwebsite/
with your frontend url (home page url)
Finally, click on Go button to execute the command
If the commands were executed successfully, you will see this notification: 1 row affected (Querry took 0.0717 seconds)
Method 2: Update URL using terminal
You can also update URL using MYSQL command in terminal.
First, click on Shell to open terminal
Login to MySQL using this command
mysql -u root -p
Next, enter your root password and run the following commands
UPDATE `core_config_data` SET `value` = 'http://127.0.0.1/yourwebsite/' WHERE `core_config_data`.`path` = 'web/unsecure/base_url';
UPDATE `core_config_data` SET `value` = 'http://127.0.0.1/yourwebsite/' WHERE `core_config_data`.`path` = 'web/secure/base_url';
Finally, clean cache
php bin/magento cache:clean
Now you will be able to add product to cart normally
Optional method
In some cases, remove the var/cache folder in your Magento 2 root folder will solve this issue.
Wrapping up
This error happens only on local environment and it has been fixed since Magento 2.4. However, there are many Magento 2 users still working on 2.3 version, so I hope this article will be helpful for your reference.
If you still facing the issue after following the solution in this tutorial, please drop a comment below describe your problem. I will be happy to help!
1 Comment
Dear Mr. Tran,
sorry my magento 2.4.3 doesn’t add these products to the cart with your solution (with cache:flush and cache:clean)
I have an local-area on win10.
Did you prefer another another way to solve the problem?
Thanks alot.