Answers.Body: <p>Since multiple line item can be updated or added to the cart at the same time, it would be hard to point to a specific line item that is newest.</p>
  
  <p>The line items have a <code>dateUpdate</code> column you could sort and query on.</p>
  
  <pre><code>{% set result = craft.query()
      .select('*')
      .from('{{%commerce_lineitems}}')
      .where({
          'orderId': cart.id
      })
      .orderBy('dateUpdated ASC')
      .one()
  %}
  </code></pre>
  
  <p>We could look to added a <code>lastUpdateLineItem()</code> method to the order element in future version of Craft Commerce. You can submit the idea here: <a href="https://github.com/craftcms/commerce/issues" rel="nofollow noreferrer">https://github.com/craftcms/commerce/issues</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-11T10:18:27.893Z
Answers.Text: Perfect, thanks Luke. And yes, I see how multiple lines could cause a problem, such as changing quantities etc, but as far as adding items to the cart, I expect the majority of sites add one item at a time (regardless of the qty).
Answers.UserDisplayName: Seraphim Jester
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-12T13:20:22.843Z
Answers.Text: I've tried this out and it seems that .orderBy('...ASC') and ('..DESC') return the first lineItem added I believe because the cart updates all line items together. I tried using dateCreated but this doesn't work if I go back to add additional items to a previously added lineItem
Answers.UserDisplayName: Seraphim Jester
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-30T08:20:32.630Z
Answers.Text: @lukeholder did `lastUpdateLineItem` ever get added to commerce? If not is this something you’d still consider adding?
Answers.UserDisplayName: Terry Upton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-08T09:47:03.727Z
Answers.LastActivityDate: 2019-07-08T09:47:03.727Z
Answers.UserDisplayName: Luke Holder
Answers.Body: <p>If your use-case is where you <em>do</em> only allow a single product at a time to be added to the cart, then one simple way I think you could do this is just take the first item in the <code>lineItems</code> array. I say this because <em>it seems</em> that whenever a product gets added to the cart, it becomes the first item in the array.</p>
  
  <p>I can't find any documentation on this but I suspect this is how Commerce was designed as that would reflect most user's expectations that the most recent thing they add is typically shown first in a list of products in their cart.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-04T11:47:45.473Z
Answers.Text: Makes sense @Mike, but I have a feeling this may break with updated qty etc.. but I like the idea, and may well do the job!
Answers.UserDisplayName: Seraphim Jester
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-05T17:54:06.403Z
Answers.Text: Actually, I don't think it does break with a quantity update... though I haven't tested thoroughly it seems the order of item does not change.
Answers.UserDisplayName: Mike
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-30T08:21:27.840Z
Answers.Text: @mike I’m trying to do the same thing to get the last added item. What was you final approach/solution?
Answers.UserDisplayName: Terry Upton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-30T20:57:56.817Z
Answers.LastActivityDate: 2020-01-30T20:57:56.817Z
Answers.UserDisplayName: Mike
Body: <p>Does anyone know a nice way to get the last lineItem(s) added to a cart in craft commerce via twig template? Example use case, an 'added to cart' confirmation page. I know for this you could query param a product/variant id but multiple could be submitted.</p>
  
  <p>I attempted <code>{cart.lineItems | last}</code> but they do not seem to be in date order. I also checked the class models but didn't find any helpful methods or attributes. </p>
  
  <p>Perhaps some kind of db query, as sql has date fields?</p>
  
Tags: <craft3><plugin-craftcommerce><plugin-craftcommerce2><mysql>
Title: Get last item added to cart
ThreadText: # Get last item added to cart
  
  (Asked by: username_0 on 2019-07-08)
  
  Does anyone know a nice way to get the last lineItem(s) added to a cart in craft commerce via twig template? Example use case, an 'added to cart' confirmation page. I know for this you could query param a product/variant id but multiple could be submitted.
  
  I attempted `{cart.lineItems | last}` but they do not seem to be in date order. I also checked the class models but didn't find any helpful methods or attributes.
  
  Perhaps some kind of db query, as sql has date fields?
  
  **Tags:** craft3, plugin-craftcommerce, plugin-craftcommerce2, mysql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-07-08. Score: 2
  
  Since multiple line item can be updated or added to the cart at the same time, it would be hard to point to a specific line item that is newest.
  
  The line items have a `dateUpdate` column you could sort and query on.
  
  ```
  {% set result = craft.query()
      .select('*')
      .from('{{%commerce_lineitems}}')
      .where({
          'orderId': cart.id
      })
      .orderBy('dateUpdated ASC')
      .one()
  %}
  
  ```
  
  We could look to added a `lastUpdateLineItem()` method to the order element in future version of Craft Commerce. You can submit the idea here: <https://github.com/craftcms/commerce/issues>
  
  > **Comments:**
  > **username_0**: Perfect, thanks username_1. And yes, I see how multiple lines could cause a problem, such as changing quantities etc, but as far as adding items to the cart, I expect the majority of sites add one item at a time (regardless of the qty).  
  > **username_0**: I've tried this out and it seems that .orderBy('...ASC') and ('..DESC') return the first lineItem added I believe because the cart updates all line items together. I tried using dateCreated but this doesn't work if I go back to add additional items to a previously added lineItem  
  > **username_2**: @username_1 did `lastUpdateLineItem` ever get added to commerce? If not is this something you’d still consider adding?  
  
  ---
  
  ### Answer by username_3 on 2020-01-30. Score: 1
  
  If your use-case is where you *do* only allow a single product at a time to be added to the cart, then one simple way I think you could do this is just take the first item in the `lineItems` array. I say this because *it seems* that whenever a product gets added to the cart, it becomes the first item in the array.
  
  I can't find any documentation on this but I suspect this is how Commerce was designed as that would reflect most user's expectations that the most recent thing they add is typically shown first in a list of products in their cart.
  
  > **Comments:**
  > **username_0**: Makes sense @username_3, but I have a feeling this may break with updated qty etc.. but I like the idea, and may well do the job!  
  > **username_3**: Actually, I don't think it does break with a quantity update... though I haven't tested thoroughly it seems the order of item does not change.  
  > **username_2**: @username_3 I’m trying to do the same thing to get the last added item. What was you final approach/solution?  
  
  ---
  
  