Answers.Body: <p>Can you try using <code>EVENT_POPULATE_LINE_ITEM</code>? Not sure, but it fired AFTER  the product &amp; variant snapshot event. Here you can take any necessary fields &amp; data, including one from the snapshot.</p>
  <pre><code>    use yii\base\Event;
      use craft\commerce\events\LineItemEvent;
      use craft\commerce\services\LineItems;
  
      ...
  
      Event::on(
          LineItems::class,
          LineItems::EVENT_POPULATE_LINE_ITEM,
          function (LineItemEvent $e) {
              $lineItem = $e-&gt;lineItem;
              $snapshot = $lineItem-&gt;snapshot;
              $order = $lineItem-&gt;order;
              // ....
  
              // Changing some values
              $lineItem-&gt;privateNote = 'Testing';
          }
      );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-30T22:16:45.677Z
Answers.LastActivityDate: 2023-04-01T08:15:32.507Z
Answers.LastEditDate: 2023-04-01T08:15:32.507Z
Answers.UserDisplayName: RomanAvr
Body: <p>I wrote a function to sort / re-order the line items in a cart.</p>
  <pre class="lang-php prettyprint-override"><code>function sortCart(Order $order) {
      $lineItems = $order-&gt;getLineItems();
      $lineItemsOrdered = array();
  
      // [...]
  
      $order-&gt;setLineItems($lineItemsOrdered);
  }
  </code></pre>
  <p>it works, but obviously this only changes the in-memory order object.
  how can I make it so that the order is also persisted in the database? I would like to see the line items in the modified order in the craft backend.</p>
  <hr />
  <p>edit:</p>
  <p>the sorting logic depends on a field set in the <code>EVENT_AFTER_CAPTURE_PRODUCT_SNAPSHOT</code> event, so <code>EVENT_BEFORE_SAVE</code> won't work because it runs earlier.</p>
  <p><code>Craft::$app-&gt;getElements()-&gt;saveElement($order, false);</code> in an adjuster will cause an endless loop.</p>
  <p>how do I do this?</p>
  <hr />
  <p>edit:
  apparently the order is determined by the creation date of the line items. however this doesn't work either:</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(
      Order::class,
      Order::EVENT_BEFORE_SAVE,
      function (ModelEvent $event) {
          $order = $event-&gt;sender;
          sortCart($order); // also sets `dateCreated` on line item
  
          // log shows the correct order/timestamps
          foreach ($order-&gt;getLineItems() as $item) {
              Module::info($item-&gt;dateCreated-&gt;format(\DateTime::ATOM) . ': ' . $item-&gt;getDescription());
          }
      }
  );
  </code></pre>
  
Tags: <plugin-craftcommerce><order><cart>
Title: How to persist modified order of cart line items?
ThreadText: # How to persist modified order of cart line items?
  
  (Asked by: username_0 on 2023-03-30)
  
  I wrote a function to sort / re-order the line items in a cart.
  
  ```php
  function sortCart(Order $order) {
      $lineItems = $order->getLineItems();
      $lineItemsOrdered = array();
  
      // [...]
  
      $order->setLineItems($lineItemsOrdered);
  }
  
  ```
  
  it works, but obviously this only changes the in-memory order object.
  how can I make it so that the order is also persisted in the database? I would like to see the line items in the modified order in the craft backend.
  
  ---
  
  edit:
  
  the sorting logic depends on a field set in the `EVENT_AFTER_CAPTURE_PRODUCT_SNAPSHOT` event, so `EVENT_BEFORE_SAVE` won't work because it runs earlier.
  
  `Craft::$app->getElements()->saveElement($order, false);` in an adjuster will cause an endless loop.
  
  how do I do this?
  
  ---
  
  edit:
  apparently the order is determined by the creation date of the line items. however this doesn't work either:
  
  ```php
  Event::on(
      Order::class,
      Order::EVENT_BEFORE_SAVE,
      function (ModelEvent $event) {
          $order = $event->sender;
          sortCart($order); // also sets `dateCreated` on line item
  
          // log shows the correct order/timestamps
          foreach ($order->getLineItems() as $item) {
              Module::info($item->dateCreated->format(\DateTime::ATOM) . ': ' . $item->getDescription());
          }
      }
  );
  
  ```
  
  **Tags:** plugin-craftcommerce, order, cart
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-03-30. Score: 1
  
  Can you try using `EVENT_POPULATE_LINE_ITEM`? Not sure, but it fired AFTER the product & variant snapshot event. Here you can take any necessary fields & data, including one from the snapshot.
  
  ```
      use yii\base\Event;
      use craft\commerce\events\LineItemEvent;
      use craft\commerce\services\LineItems;
  
      ...
  
      Event::on(
          LineItems::class,
          LineItems::EVENT_POPULATE_LINE_ITEM,
          function (LineItemEvent $e) {
              $lineItem = $e->lineItem;
              $snapshot = $lineItem->snapshot;
              $order = $lineItem->order;
              // ....
  
              // Changing some values
              $lineItem->privateNote = 'Testing';
          }
      );
  
  ```
  
  
  ---
  
  