Ecommerce for Statamic

Creating a Sales Overview Page

So you’ve made some sales and your order folder is rapidly filling up with entries. Great! It’s fun to browse through them and look at the totals, then enter them into your calculator one by one. Wait - no it’s not.

There’s a better way. They are entries after all.

Currently, there’s no built-in sales reporting in Bison. (Hint: it’s planned) But you can easily create a page to view whatever you like at a glance fairly easily.

The page

Let’s say you want yoursite.com/sales to display all your hard earned dollars.
Create _content/sales.md or _content/sales/page.md.

---
title: Sales Overview
_template: sales_overview
---

The template

Create _themes/templates/sales_overview.html.

{{ entries:listing folder="orders" sort_by="order_date" sort_dir="asc" }}
  {{ if no_results }}
    <p>No orders have been placed.</p>
  {{ else }}
    {{ if first }}
      <table>
        <thead>
          <tr>
            <th>Order</th>
            <th>Customer</th>
            <th>Date</th>
            <th>Total</th>
          </tr>
        </thead>
        <tbody>
    {{ endif }}
          <tr>
            <td><a href="{{ url }}">{{ title }}</a></td>
            <td>{{ first_name }} {{ last_name }}</td>
            <td>{{ order_date format="j M Y" }}</td>
            <td>${{ total }}</td>
          </tr>
    {{ if last }}
        </tbody>
      </table>
    {{ endif }}
  {{ endif }}
{{ /entries:listing }}

Now you have a table of orders that looks something like this:

Order Customer Date Total
Order #1 John Smith 20 Jan 13 $10.00
Order #2 Jane Doe 13 Feb 13 $20.00

Adding it all up

You might not need a masters in math to work out the total here, but let’s make it easier anyway.

Statamic 1.7 introduced the shiny new entries:meld tag. It can perform tasks across multiple entries. One of which is sum.
You can add this to your template to get to total of all your orders.

Total:
${{ entries:meld folder="orders" field="total" action="sum" precision="2" }}

You now have a complete order listing with a grand total! Look how much you’ve made!
…But you probably don’t want anyone else to see.

Securing the page

The Statamic guys have been busy. They’ve also added a way to protect your content from unwanted guests. This is done through the new _protect variable. It goes in your front-matter.

Back in your .md file:

---
title: Sales Overview
_template: sales_overview
_protect:
  allow:
    roles: [ admin ]
---

What that says is that you are only allowing members with the role of admin to view this page. Anyone that doesn’t fit this criteria is sent to a no-access page. Or, if they aren’t logged in, they’ll be taken to a login page.
The URLs of the login and no access pages are set in the Protect plugin’s config file.

The full page

Now you have a completely secure page where you can view all your sales in one go. Remember, orders are just entries, so you can show as much or as little detail as you like. You aren’t limited to what’s been demonstrated here. You could show each item in the order, paginate the orders, show orders for a specific member. The list goes on.

Comments