[4.x]: Multiple empty carts when refreshing fast with autoSetNewCartAddresses setting
@lukeholder is already working on this.
Since Apr 15, 2025.
- Dominant language
- PHP
- Stars
- 240
- Forks
- 173
- Avg merge
- 5d 11h
- Merged PRs (30d)
- 8
Description
What happened?
Description
New issue in favor of https://github.com/craftcms/commerce/issues/3715 because the problem still exists.
When the autoSetNewCartAddresses is enabled, and you've set a default primary address in your account (yes
this only applies when you are logged in), it is possible to create multiple empty carts on refresh.
This default code from the example folder:
{% if cart is not defined %}
{% set cart = craft.commerce.carts.cart %}
{% endif %}
is creating multiple carts when you have the autoSetNewCartAddresses setting enabled and you've setup a default primary address. This happens when you refresh several times in a row. You have to refresh fast but apparently customers are doing this because we are seeing multiple empty carts in our shop.
This only happens when the user has no active carts.
When debugging I found out that this is caused because of the autoSetNewCartAddresses setting. This is causing a save on first reload when you have no active cart. So when reloading multiple times it creates multiple
carts:
Steps to reproduce
- Do a clean install with craft 4.x and copy the commerce example-templates
- Create a account and log in.
- Set a default primary address
- Open a tab with the /shop/ page.
- Clear all active carts
- Go to the /shop/ tab and refresh fast, like multiple times per second
- You will see multiple empty active carts in the control panel tied to the same user.
Expected behavior
The expected behavior is that Craft Commerce only creates 1 cart.
Actual behavior
Craft Commerce creates multiple empty carts.
Craft CMS version
4.14.14
Craft Commerce version
4.8.3
PHP version
8.2
Operating system and version
Mac os 15.2 (24C101)
Database type and version
MySQL 8
Image driver and version
No response
Installed plugins and versions
- Craft Commerce
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.