Answers.Body: <p>Turns out I just replace:</p>
  
  <p><code>$cart-&gt;addLineItem($lineItem);</code></p>
  
  <p>With:</p>
  
  <p><code>CommercePlugin::getInstance()-&gt;getLineItems()-&gt;saveLineItem($lineItem, false);</code></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-11T21:55:31.940Z
Answers.Text: is there a way to remove a line items from Plugin instance?
Answers.UserDisplayName: Ezequiel Fernandez
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-19T11:09:34.277Z
Answers.LastActivityDate: 2019-03-19T11:09:34.277Z
Answers.UserDisplayName: Jason Mayo
Body: <p>I'm currently trying to adjust the line items in a particular cart/order after an event is triggered.</p>
  
  <p>I've got:</p>
  
  <pre><code>use craft\commerce\Plugin as CommercePlugin;
  
  $this-&gt;requirePostRequest();
  $request = Craft::$app-&gt;getRequest();
  
  $cart = CommercePlugin::getInstance()-&gt;getCarts()-&gt;getCart(true);
  
  $lineItem = CommercePlugin::getInstance()-&gt;getLineItems()-&gt;getLineItemById($cart-&gt;lineItems[0]-&gt;id);
  
  $lineItem-&gt;price = 100.0000;
  $lineItem-&gt;salePrice = 100.0000;
  $lineItem-&gt;qty = 1;
  $lineItem-&gt;note = '';
  
  $cart-&gt;addLineItem($lineItem);
  
  $cart-&gt;email = $request-&gt;getParam('orderEmail');
  
  if (!Craft::$app-&gt;getElements()-&gt;saveElement($cart)) {
     return false;
  }
  </code></pre>
  
  <p>But this seems to not do anything, except update <code>$lineItem</code> and not update the line item when the cart is then saved.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-15T16:36:47.727Z
Comments.Text: Any errors on the cart after saving?
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-19T10:59:26.400Z
Comments.Text: @LukeHolder No errors, and the flash just returns "Cart Updated"
Comments.UserDisplayName: Jason Mayo
Tags: <plugin-craftcommerce><plugin-craftcommerce2><ecommerce>
Title: Dynamically update line item in Commerce 2
ThreadText: # Dynamically update line item in Commerce 2
  
  (Asked by: username_0 on 2019-03-15)
  
  I'm currently trying to adjust the line items in a particular cart/order after an event is triggered.
  
  I've got:
  
  ```
  use craft\commerce\Plugin as CommercePlugin;
  
  $this->requirePostRequest();
  $request = Craft::$app->getRequest();
  
  $cart = CommercePlugin::getInstance()->getCarts()->getCart(true);
  
  $lineItem = CommercePlugin::getInstance()->getLineItems()->getLineItemById($cart->lineItems[0]->id);
  
  $lineItem->price = 100.0000;
  $lineItem->salePrice = 100.0000;
  $lineItem->qty = 1;
  $lineItem->note = '';
  
  $cart->addLineItem($lineItem);
  
  $cart->email = $request->getParam('orderEmail');
  
  if (!Craft::$app->getElements()->saveElement($cart)) {
     return false;
  }
  
  ```
  
  But this seems to not do anything, except update `$lineItem` and not update the line item when the cart is then saved.
  
  **Tags:** plugin-craftcommerce, plugin-craftcommerce2, ecommerce
  
  > **Comments:**
  > **username_1**: Any errors on the cart after saving?  
  > **username_0**: @username_1 No errors, and the flash just returns "Cart Updated"  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-03-19. Score: 2 | Accepted answer
  
  Turns out I just replace:
  
  `$cart->addLineItem($lineItem);`
  
  With:
  
  `CommercePlugin::getInstance()->getLineItems()->saveLineItem($lineItem, false);`
  
  > **Comments:**
  > **username_2**: is there a way to remove a line items from Plugin instance?  
  
  ---
  
  