Skip to content

Instantly share code, notes, and snippets.

@henri-hulski
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henri-hulski/35554d2a6321547bc787 to your computer and use it in GitHub Desktop.
Save henri-hulski/35554d2a6321547bc787 to your computer and use it in GitHub Desktop.
Cartridge Donation Product
from __future__ import unicode_literals
from django.http import HttpResponseRedirect
from mezzanine.pages.page_processors import processor_for
from cartridge.shop.models import Product
from cartridge.shop.forms import AddProductForm
from .utils import Donation
@processor_for("^product/(?P<slug>.*)$")
def donation_form(request, page):
form = AddProductForm()
if request.method == "POST" and form.is_valid():
product = Product.objects.get(id=request.POST.get("product_id"))
item = Donation(product, float(request.POST.get("donation_amount", "0.0")))
request.cart.add_item(item, 1)
redirect = request.path + "?submitted=true"
return HttpResponseRedirect(redirect)
return {"form": form}
{% extends "shop/base.html" %}
{% load staticfiles mezzanine_tags shop_tags rating_tags i18n %}
{% block meta_title %}{{ product.meta_title }}{% endblock %}
{% block body_id %}category{% endblock %}
{% block meta_keywords %}{% metablock %}
{% for keyword in product.keywords.all %}
{% if not forloop.first %}, {% endif %}
{{ keyword }}
{% endfor %}
{% endmetablock %}{% endblock %}
{% block meta_description %}{% metablock %}
{{ product.description }}
{% endmetablock %}{% endblock %}
{% block extra_css %}
{{ block.super }}
<link rel="stylesheet" href="{% static "mezzanine/css/magnific-popup.css" %}">
{% endblock %}
{% block extra_js %}
{{ block.super }}
<script src="{% static "mezzanine/js/magnific-popup.js" %}"></script>
<script>
$(document).ready(function() {
$('#product-images-large').magnificPopup({
delegate: 'a',
type: 'image',
gallery: {
enabled: true,
}
});
});
</script>
{% endblock %}
{% block extra_head %}
<script>var variations = {{ variations_json|safe }};</script>
<script src="{% static "cartridge/js/product_variations.js" %}"></script>
{% endblock %}
{% block breadcrumb_menu %}
{{ block.super }}
<li>{{ product.title }}</li>
{% endblock %}
{% block title %}
{% editable product.title %}{{ product.title }}{% endeditable %}
{% endblock %}
{% block main %}
{% if images %}
{% spaceless %}
<ul id="product-images-large" class="list-unstyled list-inline">
{% for image in images %}
<li id="image-{{ image.id }}-large"{% if not forloop.first %}style="display:none;"{% endif %}>
<a class="product-image-large" href="{{ MEDIA_URL }}{{ image.file }}">
<img alt="{{ image.description }}" src="{{ MEDIA_URL }}{% thumbnail image.file 0 300 %}" class="img-thumbnail img-responsive col-xs-12">
</a>
</li>
{% endfor %}
</ul>
{% if images|length != 1 %}
<ul id="product-images-thumb" class="list-unstyled list-inline">
{% for image in images %}
<li>
<a class="thumbnail" id="image-{{ image.id }}" href="{{ MEDIA_URL }}{{ image.file }}">
<img alt="{{ image.description }}" src="{{ MEDIA_URL }}{% thumbnail image.file 75 75 %}">
</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endspaceless %}
{% endif %}
{% editable product.content %}
{{ product.content|richtext_filters|safe }}
{% endeditable %}
{% if product.available and has_available_variations %}
<ul id="variations" class="list-unstyled">
{% for variation in variations %}
<li id="variation-{{ variation.sku }}"
{% if not variation.default %}style="display:none;"{% endif %}>
{% if variation.has_price %}
{% if variation.on_sale %}
<span class="old-price">{{ variation.unit_price|currency }}</span>
{% trans "On sale:" %}
{% endif %}
<span class="price">{{ variation.price|currency }}</span>
{% else %}
{% if has_available_variations %}
<span class="error-msg">
{% trans "The selected options are currently unavailable." %}
</span>
{% endif %}
{% endif %}
</li>
{% endfor %}
</ul>
{% errors_for add_product_form %}
<form method="post" id="add-cart" action="{{ item.product.get_absolute_url }}">
{% csrf_token %}
<input type="hidden" id="product_id" name="product_id" value="{{ item.product.id }}">
<input type="hidden" name="quantity" id="id_quantity" value="1">
<input type="text" class="donation" id="donation_amount" name="donation_amount">
<button type="submit" class="button small">{% trans "Donate Now" %}</button>
</form>
{% else %}
<p class="error-msg">{% trans "This product is currently unavailable." %}</p>
{% endif %}
{% if settings.SHOP_USE_RATINGS %}
<div class="panel panel-default rating">
<div class="panel-body">{% rating_for product %}</div>
</div>
{% endif %}
{% if settings.SHOP_USE_RELATED_PRODUCTS and related_products %}
<h2>{% trans "Related Products" %}</h2>
<div class="row related-products">
{% for product in related_products %}
<div class="col-xs-6 col-sm-4 col-md-3 product-thumb">
<a class="thumbnail" href="{{ product.get_absolute_url }}">
{% if product.image %}
<img src="{{ MEDIA_URL }}{% thumbnail product.image 90 90 %}">
{% endif %}
<div class="caption">
<h6>{{ product }}</h6>
<div class="price-info">
{% if product.has_price %}
{% if product.on_sale %}
<span class="old-price">{{ product.unit_price|currency }}</span>
{% trans "On sale:" %}
{% endif %}
<span class="price">{{ product.price|currency }}</span>
{% else %}
<span class="coming-soon">{% trans "Coming soon" %}</span>
{% endif %}
</div>
</div>
</a>
</div>
{% endfor %}
</div>
{% endif %}
{% endblock %}
from __future__ import unicode_literals
class Donation(object):
def __init__(self, product, unit_price):
product_variation = product.variations.all()
self.description = product.title
self.unit_price = unit_price
self.url = product.get_absolute_url()
self.sku = product_variation[0].sku
self.product = product
self.image = product_variation[0].image
def price(self):
return self.unit_price
def __unicode__(self):
return self.description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment