<p>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.</p>

<pre><code>{% 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

    &lt;h3&gt;{{ product.title }}&lt;/h3&gt;
    &lt;p&gt;{{ product.description }}&lt;/p&gt;
    &lt;a href="purchase/product/path?id={{ product.id }}"&gt;Purchase&lt;/a&gt;

{% else %}

     {# output entry #}

{% endif %}
</code></pre>

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

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

<p>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.</p>

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

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

<p>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 <a href="http://buildwithcraft.com/classreference/services/UsersService#saveUser-detail" rel="nofollow">usersService saveUser method</a> for more info or search this site for many examples.</p>

<p>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 <a href="https://snipcart.com/blog/managing-your-craft-inventory-using-snipcart-webhooks" rel="nofollow">managing your craft inventory with snip cart</a>, which should describe everything you need to create the webhook.</p>

CC BY-SA 3.0
2015-02-24T02:46:23.927Z
Thank you for the great answer Douglas. I am going to try this method hopefully tomorrow. I really appreciate your time.
Bryan Garrant
CC BY-SA 3.0
2015-02-24T04:00:08.533Z
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.
Douglas McDonald
CC BY-SA 3.0
2015-02-23T20:58:38.787Z
2015-02-24T00:19:31.740Z
2015-02-24T00:19:31.740Z
Douglas McDonald
<p>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. </p>

<p>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?</p>

<p>Thanks</p>

<p>Bryan</p>

<ecommerce>
How sell products with Craft and secure the product viewing pages?
# How sell products with Craft and secure the product viewing pages?

(Asked by: username_0 on 2015-02-23)

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

**Tags:** ecommerce

---

## 1 answers

### Answer by username_1 on 2015-02-23. Score: 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

    {{ product.title }}
    {{ product.description }}
    Purchase

{% else %}

     {# output entry #}

{% endif %}

To create an index page of all purchased entries, you can loop through the currentUsers purchased entries.
{% if currentUser %}
    
        {% for entry in currentUser.purchasedProducts %}
            {{ entry.title }}
        {% else %}
            You have not purchased any products.
        {% endfor %}
    
{% 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 %}


    {% for product in craft.entries.section('products') %}
        
            {{ product.title }}
            {{ product.description }}
            {% if product.id in purchasedProductsIds %}
                View
            {% else %}
                Purchase
            {% endif %}
    {% endfor %}


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.

> **Comments:**
> **username_0**: Thank you for the great answer Douglas. I am going to try this method hopefully tomorrow. I really appreciate your time.  
> **username_1**: 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.  

---

