craftcms / craftcms/commerce

[4.x]: Multiple empty carts when refreshing fast with autoSetNewCartAddresses setting

Open
#3,973 6 comments 0 reactions 1 assignee View on GitHub

@lukeholder is already working on this.

Since Apr 15, 2025.

bug commerce4 Craft Commerce
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:

Image
Steps to reproduce
  1. Do a clean install with craft 4.x and copy the commerce example-templates
  2. Create a account and log in.
  3. Set a default primary address
  4. Open a tab with the /shop/ page.
  5. Clear all active carts
  6. Go to the /shop/ tab and refresh fast, like multiple times per second
  7. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.