<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>

CC BY-SA 4.0
2024-04-11T21:55:31.940Z
is there a way to remove a line items from Plugin instance?
Ezequiel Fernandez
CC BY-SA 4.0
2019-03-19T11:09:34.277Z
2019-03-19T11:09:34.277Z
Jason Mayo
<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>

CC BY-SA 4.0
2019-03-15T16:36:47.727Z
Any errors on the cart after saving?
Luke Holder
CC BY-SA 4.0
2019-03-19T10:59:26.400Z
@LukeHolder No errors, and the flash just returns "Cart Updated"
Jason Mayo
<plugin-craftcommerce><plugin-craftcommerce2><ecommerce>
Dynamically update line item in Commerce 2
# 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**: @LukeHolder 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?  

---

