Purchasables
The core of anything sold with Commerce is a purchasable, a special type of element that conforms to an interface that guarantees it can be added to a cart.
Commerce ships with two types of purchasables: variants and donations. Somewhat counterintuitively, products are not purchasables!
# Line Items
When a customer adds a purchasable to their cart, Commerce represents it as a line item.
Line items capture a reference to the purchasable and some vital properties like the price, dimensions, weight. They can also hold customer notes, private notes, and arbitrary options data.
# Snapshots
When a line item is populated from a purchasable, a JSON snapshot is saved on that line item. The snapshot provides a permanent record of whatever the purchasable looked like in that moment, regardless of how products or variations change over time. The snapshot will persist, regardless of any store changes after checkout.
Snapshot data can be essential in cases where you need to display a completed order’s line items in a template but the product and variant have since been deleted. This only applies to completed orders, though—if a purchasable is deleted prior to checkout, any line items that refer to it are automatically removed from customers’ carts and notices are attached with a description of what happened.
When building your front end and displaying line items, it’s best to reference purchasable information retrieved via lineItem.getPurchasable()
. The snapshot
property is a plain array, and will not have many of the variant (opens new window) and product (opens new window) methods you would expect, having dealt with them elsewhere in your templates:
{# Set a fallback for the purchasable info: #}
{% set purchasable = lineItem.getPurchasable() ?? lineItem.snapshot %}
To avoid recursive references and other serialization issues, snapshots do not automatically include custom field data for variants or products. You can customize what additional fields are stored in a snapshot via the Variant::EVENT_BEFORE_CAPTURE_VARIANT_SNAPSHOT (opens new window) event, or add arbitrary data in reponse to Variant::EVENT_AFTER_CAPTURE_VARIANT_SNAPSHOT (opens new window). Similar events exist for product elements, as well.
Snapshots are not an authoritative source for pricing, stock, availability, or other values that can change—they are only intended to serve as way of identifying a purchasable to the customer (and administrators) after an order is completed.
To learn more about adding your own custom purchasables, see the Purchasable Types page.