1

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.

2
  • 1
    Any errors on the cart after saving? Mar 15, 2019 at 16:36
  • @LukeHolder No errors, and the flash just returns "Cart Updated"
    – Jason Mayo
    Mar 19, 2019 at 10:59

1 Answer 1

2

Turns out I just replace:

$cart->addLineItem($lineItem);

With:

CommercePlugin::getInstance()->getLineItems()->saveLineItem($lineItem, false);

1
  • is there a way to remove a line items from Plugin instance? Apr 11 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.