Add items to user's cart in Craft Commerce 2 I'm trying to add items to user's cart from a plugin but I'm stuck on getting cart of a user other then current user.
I have the code for adding items to cart, it's something I used on current user. But I don't know how to get the cart for any user.
&lt;?php
$cart = iHaveNoIdeaHowToGetUsersCart($user-&gt;id); // &lt;---
// I used this for current user:
// $cart = craft\commerce\Plugin::getInstance()-&gt;getCarts()-&gt;getCart();
if ($cart-&gt;id == null) {
    // Create cart if it does not exist
    Craft::$app-&gt;getElements()-&gt;saveElement($cart);
}

$purchasableId = Craft::$app-&gt;getGlobals()-&gt;getSetByHandle('booking')-&gt;breakProduct-&gt;one()-&gt;id;
$options = [
    'amount' =&gt; (float)$something,
    'description' =&gt; 'Something'
];
$newLineItem = Plugin::getInstance()-&gt;getLineItems()-&gt;createLineItem($cart-&gt;id, $purchasableId, $options, 1);
$cart-&gt;setLineItems([$newLineItem]);
Craft::$app-&gt;getElements()-&gt;saveElement($cart);
?&gt;
In Commerce, a User (or guest with an email address for that matter) can have multiple carts (or unfinished orders).
So we need to get the most recently active one. Something like this should work:
$query = Order::find();
$query-&gt;id($whateverId); // you could also use $query-&gt;email 
$query-&gt;isCompleted(false);
$query-&gt;orderBy('dateCreated DESC');
$cart =  $query-&gt;one();

... // rest of your code