5

I have a client that wants to sell products (digital content) that are only viewable in templates. These are not PDFs. The template page will have content on it that is only available to people that purchased the product. This is not a subscription, but a product sale with unlimited viewing after purchase via a secure login area.

What would be a good Ecommerce option for this project as there could be multiple products purchased at once? How can I make it so only validated purchasers can see the template content of the products purchased? Snipcart looks cool but no idea how to secure the templates for valid purchasers. Any better way to do this?

Thanks

Bryan

1 Answer 1

2

I can only really comment on securing content as I haven't used any ecommerce solutions with craft. But as far as that is concerned — one approach would be to add a related entries field-type on the users' field layout (called 'purchasedProducts'). When a user purchases a product you can add that entry to their purchased products via a plugin. You can then check the current entry against the related entries to determine if the user is authorized to view the content.

{% set authorized = false %}
{% if currentUser %}
    {% if entry.id in currentUser.purchasedProducts.ids() %}
        {% set authorized = true %}
    {% endfor %}
{% endif %}

{% if not authorized %}

    {% redirect 403 %} 

    // or display product teaser and purchase button

    <h3>{{ product.title }}</h3>
    <p>{{ product.description }}</p>
    <a href="purchase/product/path?id={{ product.id }}">Purchase</a>

{% else %}

     {# output entry #}

{% endif %}

To create an index page of all purchased entries, you can loop through the currentUsers purchased entries.

{% if currentUser %}
    <ul>
        {% for entry in currentUser.purchasedProducts %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% else %}
            <li>You have not purchased any products.</li>
        {% endfor %}
    </ul>
{% endif %}

To show all products and add a 'purchase' or 'view' button depending on whether they've purchased the product or not, you can check if the entry is in a 'purchasedProductsIds' array.

{% set purchasedProductsIds = [] %}
{% if currentUser %}
    {% set purchasedProductsIds = currentUser.purchasedProducts.ids() %}
{% endif %}

<ul>
    {% for product in craft.entries.section('products') %}
        <li>
            <h3>{{ product.title }}</h3>
            <p>{{ product.description }}</p>
            {% if product.id in purchasedProductsIds %}
                <a href="{{ product.url}}">View</a>
            {% else %}
                <a href="purchase/product/path?id={{ product.id }}">Purchase</a>
            {% endif %}
    {% endfor %}
</ul>

Obviously you will also need to add a small plugin to respond to the purchase process (via a hook or event) and update the user's purchasedProducts. Check out usersService saveUser method for more info or search this site for many examples.

As far as ecommerce solutions, I haven't used any with craft so I can't really comment. However, I did find a tutorial on managing your craft inventory with snip cart, which should describe everything you need to create the webhook.

2
  • Thank you for the great answer Douglas. I am going to try this method hopefully tomorrow. I really appreciate your time. Commented Feb 24, 2015 at 2:46
  • Glad to help. Hope it goes well. One thing to keep in mind is that anyone with modify user privileges would be able to manipulate orders manually, if you want to put product orders someplace more secure you could also create a channel (called 'accounts' for example) that was linked to both a user and products. You could then enable/disable access to this channel separately from users. It would make the above examples slightly more complex but not too bad. Commented Feb 24, 2015 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.