Ecommerce for Statamic

Creating an Order History Section

One of the most requested (and obvious) features for Bison was the ability to create an order history section and to be able to make user-associated orders. We were going to wait until Statamic 1.7 was released before working on this, since it would include the more advanced user functions.

At the moment your users need to be manually created in the /_config/users/ directory. Statamic 1.7 will introduce the {{ member:register_form }} tag which will allow users to register themselves from the front-end. Although, you can still get ready and create your order history section now using Bison.

There is nothing really Bison-specific about doing this. It’s all done with basic Statamic code.
The latest update to Bison saves the customers user id (uid) and username (for your convenience) to the order. We’ve prepended customer_ to avoid conflicts. That’s about it.

Let’s say you want to have an order-history template which shows all orders for the currently logged in user.
Your template would look something like this:

<h1>Order History</h1>

{{ unless logged_in }}
  <p>Log in to see your order history.</p>
{{ else }}

  {{ entries:listing folder="orders" conditions="customer_uid:{{ current_member:_uid }}" }}
    {{ if no_results }}
      <p>You have not placed any orders.</p>
    {{ else }}
      {{ if first }}
      <ul>
      {{ endif }}
        <li><a href="{{ url }}">{{ title }}</a></li>
      {{ if last }}
      </ul>
      {{ endif }}
    {{ endif }}
  {{ /entries:listing }}

{{ endif }}

Then, to prevent users from seeing someone elses order, we’ve created a tag that you can use to check whether the order belongs to the current user.
Note: This tag will only work in 1.7+.

{{ unless {bison:order_belongs_to_member} }}
  <p>You can't look at other people's orders!</p>
{{ else }}
  <h1>{{ title }}</h1>
  <p>Sold to {{ first_name }} for {{ total }}</p>
{{ endif }}

There’s your order history section.

Comments