You can access your site’s orders from your templates via craft.orders
It returns an ElementQuery object.
{% set orders = craft.orders.all() %}
{% for order in orders %}
{{ order.number }} - {{ order.totalPrice}} <br>
{% endfor %}
craft.orders
supports the following parameters:
Product Type model or handle.
Product type id.
The unique hash of the order.
Accepts true
. e.g {% set orders = craft.orders.completed(true).all() %}
would
return completed orders since they have isCompleted
set to true.
Accepts 1
or not 1
. e.g {% set orders = craft.orders.isCompleted('not 1').all() %}
would
return incomplete orders (carts) since they have isCompleted
set to false.
The date the order was completed.
accepts an orderStatus
model.
Accepts the id of an Order Status.
A customer Model can be passed to get orders for that customer only. e.g {% set orders = craft.orders.customer(craft.commerce.customer).all() %}
Do not use this to get a cart, as the default response does not include orders that are still
carts (use {% set cart = craft.commerce.getCart %}
to get the current user's cart).
A customer Model can be passed to get orders for that user only. e.g {% set orders = craft.orders.user(currentUser).all() %}
Do not use this to get a cart, as the default response does not include orders that are still
carts (use {% set cart = craft.commerce.getCart %}
to get the current user's cart).
Accepts an id of a customer.
Only fetch orders with an Updated Date that is on or after the given date.
You can specify dates in the following formats:
- YYYY
- YYYY-MM
- YYYY-MM-DD
- YYYY-MM-DD HH:MM
- YYYY-MM-DD HH:MM:SS
- A Unix timestamp
- A DateTime variable
Only fetch orders with an Updated Date that is before the given date.
You can specify dates in the following formats:
- YYYY
- YYYY-MM
- YYYY-MM-DD
- YYYY-MM-DD HH:MM
- YYYY-MM-DD HH:MM:SS
- A Unix timestamp
- A DateTime variable
Accepts true
. Limits results to only orders where totalPaid is >= totalPrice
Accepts true
. Limits results to only orders where totalPaid is < totalPrice
The date the order was paid.
Returns orders that contains specific purchasables.
Accepts: An array of models meeting the Purchasable interface (like variants) OR an array of Purchasable Element IDs
For example:
{% if currentUser %}
{% set order = craft.orders.user(currentUser).hasPurchasables([product.defaultVariant]).one() %}
{% if order %}
I already own this product: <a href="shop/order?orderNumber={{ order.number }}">Order #{{ order.shortNumber }}</a>
{% endif %}
{% endif %}
or
{% set orders = craft.orders({
hasPurchasables: [32,34,35]
}) %}