Skip to content

Instantly share code, notes, and snippets.

@ddrewno
Last active August 29, 2015 14:18
Show Gist options
  • Save ddrewno/43cda21b4564ab81c6d9 to your computer and use it in GitHub Desktop.
Save ddrewno/43cda21b4564ab81c6d9 to your computer and use it in GitHub Desktop.
Create a page which is only accessible by subscribers with a particular product
{% extends "base.html" %}
{% block page_content %}
{# Customer will be 'Null' if the user is not logged in #}
{% if customer %}
{% set found = [] %}
{# status of 2 == ACTIVE #}
{% for sub in customer.subscriptions if sub.status == 2 %}
{% set latest_order = sub.orders[-1] %}
{% for p in latest_order.products %}
{# NOTE: You can put whatever id you want here, or you could even compare the product name. This provides an exact match though #}
{% if p.instance.product.id == 26352701 %}
{% do found.append(1) %}
{% endif %}
{% endfor %}
{% endfor %}
{% if found | length > 0 %}
{# This loads if the user is logged in AND has the valid product #}
ACCESS GRANTED!
{% else %}
{# This loads if the user is logged in but does NOT have the valid product (or maybe doesn't have any valid subscriptions at all) #}
NO ACCESS!
{% endif %}
{% else %}
{# This loads if the user is NOT logged in. You may want to just redirect to /customer/login in that case #}
YOU ARE NOT LOGGED IN!
{% endif %}
{% endblock %}
@ddrewno
Copy link
Author

ddrewno commented Apr 7, 2015

Please note this is an HTML file (The filename at the top is .py just so that the syntax highlighting in git is helpful)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment