How to load current user cart in Commerce 2? In Commerce 2, how can I load the current user's cart? The current user may be either anonymous or logged in. The site has multiple stores and I won't know which store the user is currently on.
It depends what you need, really. If you just need all carts for all stores, the cart provider service can give you that:

$all_carts = \Drupal::service('commerce_cart.cart_provider')
  -&gt;getCarts();


If you need a cart for a particular store based on context, it gets trickier. A reference to the "current" store can be found via the current store service:

$store = \Drupal::service('commerce_store.current_store')-&gt;getStore();


And the cart related to that store via the cart provider:

$cart = \Drupal::service('commerce_cart.cart_provider')
  -&gt;getCart('default', $store);


(incidentally in all the above snippets, if you can inject those services into your current context instead of using the static methods, all the better).

The problem is in Commerce's definition of a "current" store. Out of the box that either comes from the order entity when viewing an order, or it's the default store. From your description neither of those will do the job, so you'll need to write a custom store resolver.

I don't know what context you have available when needing the cart entity, so here's a naive example based on a product page. Since a product can be in more than one store, it just returns the first in the array.

namespace Drupal\MODULE\Resolver;

use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_store\Resolver\StoreResolverInterface;
use Drupal\Core\Routing\RouteMatchInterface;

class ProductPageStoreResolver implements StoreResolverInterface {

  protected $routeMatch;

  public function __construct(RouteMatchInterface $route_match) {
    $this-&gt;routeMatch = $route_match;
  }

  public function resolve() {
    $product = $this-&gt;routeMatch-&gt;getParameter('commerce_product');
    if ($product instanceof ProductInterface) {
      $stores = $product-&gt;getStores();
      return reset($stores);
    }
    return NULL;
  }
}


And the entry for MODULE.services.yml:

MODULE.product_page_store_resolver:
  class: Drupal\MODULE\Resolver\ProductPageStoreResolver
  arguments: ['@current_route_match']
  tags:
    - { name: commerce_store.store_resolver, priority: 100 }


Once you clear the cache, the first code snippet above will return the first store a product is assigned (when viewing it).