Purchasables
The core of anything sold with Commerce is a purchasable, a custom element type (opens new window) that can be added to a cart.
# Purchasables and Line Items
Every purchasable is destined to become a line item, which happens when the purchasable is added to a cart. This is an important shift where the purchasable describing an item’s content and available options becomes a line item in an order.
When a line item is populated from a purchasable, a JSON shapshot is saved on that line item. The snapshot provides a permanent reference to whatever the purchasable looked like in that moment, regardless of how products or variations change over time. In other words, the details in the snapshot will persist regardless of any store changes during or after the checkout process.
This can be particularly helpful, for example, if you’re displaying a completed order’s line items in templates and getPurchasable()
returns null
. This would happen if the purchasable was deleted, in which case details could be used from the snapshot instead. (This only applies to completed orders, because if a purchasable is deleted during checkout the related line item would be removed from the customer’s cart.)
When building your front end and displaying line items, it’s best to reference purchasable information through getPurchasable()
, relying the snapshot
property either as a fallback or a source of truth when the purchasable is changed or deleted.
To learn more about adding your own custom purchasables, see the Purchasable Types page.