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.

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

Turns out I just replace:

$cart->addLineItem($lineItem);

With:

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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