Merges duplicate carts when a cart is assigned to a customer, so that a customer ends up with a single cart per store and order type. The most common case is a visitor who adds products while logged out and then logs in to an account that already has a cart.

Problems it solves

In Drupal Commerce a user can own any number of carts, and Commerce Cart never merges them. When a cart is assigned to a customer who already has one, both stay in place. This leads to:

  • Split carts after login. A visitor adds products while logged out, then logs in to an account that already has a cart from an earlier visit. The products are now spread over two carts and the customer has to find and check out
    each one separately, or may not notice the second one at all.
  • Forgotten, stale carts. Old carts pile up in the account and keep showing up long after the customer has moved on.
  • Login during checkout. A customer who logs in mid-checkout should continue with the cart they started with. The module can optionally discard their other carts so that the checkout is not interrupted by leftovers.

With this module the items end up in a single cart per store and order type, and identical items are combined with their quantities added up.

This module is not a general "one cart for everything" tool. It does not merge carts of different stores or order types, and it does not touch anonymous sessions. See What this module does not do before opening an issue.

Requirements

This module requires the Commerce module. Specifically its Cart (commerce_cart) submodule.

Installation

Install as you would normally install a contributed Drupal module. For further information, see Installing Drupal Modules.

Configuration

The module works without any configuration. There is one optional setting at /admin/commerce/config/combine-carts (listed below the order settings in the Commerce configuration), which requires the administer commerce_order permission:

  • Delete non-checkout carts (off by default): If enabled, and a customer logs in during checkout, all their other carts of the same store and order type are discarded and only the cart the checkout was started with is kept.
    If disabled, the carts are merged instead.

How it works

Why a user can have several carts

In Drupal Commerce a cart is an order (commerce_order) with the cart flag set and in the draft state. A user can own any number of orders, so a user can own several carts. Commerce Cart assigns anonymous carts to the user on login, but
it never merges them, so the customer is left with several carts (see Problems it solves).

What the module does about it

The module listens to the Commerce "order assign" event (OrderEvents::ORDER_ASSIGN). Whenever a cart is assigned to a customer, every other cart that customer owns with the same order type and the same store is handled:

  • Merge (default): The items of the other cart are moved into the assigned cart, and the then-empty other cart is deleted. This happens in a database transaction, so a failure rolls everything back.
  • Discard: If "Delete non-checkout carts" is enabled and the assigned cart is the one currently being checked out, the other carts are deleted instead.

When items are merged, identical items (same purchased product variation and matching fields) are combined and their quantities added up, as long as the "Combine order items containing the same product variation" option is enabled in the add-to-cart
form display of the product ("Manage display" > "Variations"). Otherwise the items are added as separate order items. Items whose product variation no longer exists are never combined.

What this module does not do

Most issues reported against this module come from expecting more than the above.

  • It does not combine carts across stores. Commerce deliberately keeps one cart per store, because the items of different stores are paid for, shipped and taxed separately and may even use different currencies. If you run several
    stores (for example one per currency) a customer will still see one cart per store. This is how Commerce works, not a limitation of this module.
  • It does not combine carts of different order types, for the same reason.
  • It does not work for anonymous visitors. It only reacts when a cart is assigned to a user account, which for visitors happens on login. Two carts in the same anonymous session are not merged.
  • It does not clean up existing duplicates by itself. Carts are merged at the moment a cart is assigned to a customer. A customer who already has several carts keeps them until another cart is assigned to them.
  • It is not a "cart sharing" or "save for later" feature, and it has no user interface for customers.

Combining across stores or order types is not planned. If you need that, it is best implemented in a custom module or a submodule. Feel free to open an issue or merge request to discuss it.

Troubleshooting

A customer still has several carts.

  • Check that the carts have the same order type and the same store. Carts that differ in either are intentionally left alone.
  • Check that the carts are really carts (cart flag set, draft state) and belong to the customer. Orders that already went through checkout are never touched.
  • The carts may have existed before the module was installed. Assigning another cart to the customer triggers the merge.

Quantities were not added up when merging.

Enable "Combine order items containing the same product variation" in the product type's add-to-cart form display (Manage display > Variations). Without it, items are moved over as separate order items.

A customer's old cart disappeared after login.

"Delete non-checkout carts" is enabled and the customer logged in during checkout. Disable the setting to merge the carts instead.

FAQ

Q: Does it work for anonymous users?

A: No. See "What this module does not do" above.

Q: I have multiple stores and want one cart for all of them. Can this module do that?

A: No. Commerce keeps one cart per store by design, and this module respects that.

Q: Can I decide which cart "wins"?

A: The cart being assigned (usually the one created while logged out) is the one that is kept, and the other carts are merged into it. With "Delete non-checkout carts", the cart used for checkout wins.

Q: Is there an API?

A: Yes. The commerce_combine_carts.cart_unifier service (Drupal\commerce_combine_carts\CartUnifier) can be used to merge carts programmatically.

Supporting organizations: 
proudly helps developing and maintaining this module for the Drupal community

Project information

Releases