2

Having nearly completed my first real Commerce project, I managed to utilize most of the features offered in Commerce 3. However, I never quite figured out what the use of a line item's snapshot is for. What is it and when would you need to use it? There isn't anything about it in Commerce's docs.

4

The snapshot is there so that the key details of an order are forever more available on the order record.

Imagine living with Commerce for a long time - you will of course most likely add, change and/or remove products (or variations) as time goes by.

You might simply disable ones you remove, or you might delete them. If Commerce did not have line item snapshots, old orders would refer only to these now deleted things - thus, for example, when presenting an order history view to your customers, the details would not be available and this would (at least) mean some order details could no longer be displayed accurately. And at worst, it might well cause serious errors with your templates/page rendering, if you're relying on those details being there.

Another example - prices change over time. The order snapshot shows the price at the time of the sale, not the price now - which might be substantially different.

It might even be the case that store operators are altering products actually while a customer has them in their cart and is busy making an order.

Thus the snapshot mechanism is an elegant, if perhaps poorly documented, solution to these issues.

Thus - and you're right, this should probably have its own explanation in the docs - the best practise with Commerce is to always use the snapshot details by preference once something is a line item - i.e. when dealing with carts (incomplete orders) at any point, or orders - always use the snapshot data.

Of course the snapshot doesn't have everything in it (like images etc) - so for prettier templates, you often end up with some conditional logic referring to the actual product / variant - but you must check if it is still available first, before using any data from it, and have a fall-back if it is no longer available.

| improve this answer | |
  • Thank you so much for the great answer! So yeah, to be clear, basically just use the snapshot data whenever showing something that's already in the cart or when the order is completed (like in an invoice, etc). Am understanding that correctly? – Ryan Jun 24 at 11:31
  • 1
    Yep, that's exactly it. – Jeremy Daalder Jun 24 at 22:02

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.