2

I want to output a list of all orders of all customers, including customers first name and last name. But can't get these two elements.

What I have now is:

{% set pastOrders = craft.commerce.orders.find() %}
{% for order in pastOrders %}
    {% for item in order.lineItems %}
    <tr>
      <td>{{ order.id }}</td>
      <td>{{ craft.commerce.customer.firstName }}</td>
      <td>{{ craft.commerce.customer.lastName }}</td>
      <td>{{ item.description }}</td>
      <td>{{ order.email }}</td>
      <td>{{ item.options.evenement }}</td>
      <td>{{ item.note }}</td>
      <td>{{order.orderStatus.name}}</td>
      <td>{{ order.datePaid.localeDate() }}</td>
    </tr>
    {% endfor %}
  {% endfor %}

But as you may guess, I've got an error in both fields firstName lastName

3

Not all orders will necessarily have a craft user associated with the order.

But all orders must have a billing address, so:

order.billingAddress.firstName and order.billingAddress.lastName will get you what you need.

|improve this answer|||||
1

I never used Craft commerce so this answer could be totally wrong but just from looking at the docs the Order Model has the attribute customerId and the customer attribute that stores the Customer Model

customerId

The customer record for this order. You can also use customer to get the customer model which can tell you which craft user made the order, as well as access the addresses for this customer.

The customer model contains a user attribute that stores the default Craft User Model or null

user

Returns a User Model if the customer is logged in, or null if the customer is a guest.

So theoretically you should do something like

{% set customer = order.customer %}
{% if customer is not null %}
    {% set user = customer.user %}
    {% if user is not null %}
        {{ user.firstName }} {{ user.lastName }}
    {% endif %}
{% endif %}
|improve this answer|||||
  • Thanks Robin, I will try your solution. Let you know if this will work. – John Stevens Feb 24 '18 at 15:52

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.