Answers.Body: <p>In Commerce, a User (or guest with an email address for that matter) can have multiple carts (or unfinished orders).</p>
  <p>So we need to get the most recently <em>active</em> one. Something like this should work:</p>
  <pre><code>$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
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-29T18:24:25.680Z
Answers.LastActivityDate: 2021-04-29T18:24:25.680Z
Answers.UserDisplayName: RitterKnight
Body: <p>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.</p>
  <p>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.</p>
  <pre><code>&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;
  </code></pre>
  
Tags: <plugin-craftcommerce2>
Title: Add items to user's cart in Craft Commerce 2
ThreadText: # Add items to user's cart in Craft Commerce 2
  
  (Asked by: username_0 on 2021-04-28)
  
  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.
  
  ```
  <?php
  $cart = iHaveNoIdeaHowToGetUsersCart($user->id); // <---
  // I used this for current user:
  // $cart = craft\commerce\Plugin::getInstance()->getCarts()->getCart();
  if ($cart->id == null) {
      // Create cart if it does not exist
      Craft::$app->getElements()->saveElement($cart);
  }
  
  $purchasableId = Craft::$app->getGlobals()->getSetByHandle('booking')->breakProduct->one()->id;
  $options = [
      'amount' => (float)$something,
      'description' => 'Something'
  ];
  $newLineItem = Plugin::getInstance()->getLineItems()->createLineItem($cart->id, $purchasableId, $options, 1);
  $cart->setLineItems([$newLineItem]);
  Craft::$app->getElements()->saveElement($cart);
  ?>
  
  ```
  
  **Tags:** plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-29. Score: 1
  
  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->id($whateverId); // you could also use $query->email 
  $query->isCompleted(false);
  $query->orderBy('dateCreated DESC');
  $cart =  $query->one();
  
  ... // rest of your code
  
  ```
  
  
  ---
  
  