From 94908d4230954627d2ad28a88dd491c3ae390151 Mon Sep 17 00:00:00 2001
From: Nathan Chapman
Date: Sun, 13 Nov 2022 12:38:38 -0700
Subject: [PATCH] Add free shipping only to coffee
---
.../templates/dashboard/category_form.html | 2 +-
src/storefront/cart.py | 24 +++++++++++++------
.../templates/storefront/cart_detail.html | 5 +++-
.../templates/storefront/order_form.html | 4 ++++
src/storefront/tests/__init__.py | 4 ++--
src/storefront/tests/test_payments.py | 2 +-
6 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/src/dashboard/templates/dashboard/category_form.html b/src/dashboard/templates/dashboard/category_form.html
index b71aa94..d5941d5 100644
--- a/src/dashboard/templates/dashboard/category_form.html
+++ b/src/dashboard/templates/dashboard/category_form.html
@@ -10,7 +10,7 @@
{% csrf_token %}
{{form.as_p}}
- or cancel
+ or cancel
diff --git a/src/storefront/cart.py b/src/storefront/cart.py
index 0898156..af3d7fb 100644
--- a/src/storefront/cart.py
+++ b/src/storefront/cart.py
@@ -10,7 +10,7 @@ from django.core.cache import cache
from django.db.models import OuterRef, Q, Subquery
from core.models import (
- Product, ProductVariant, OrderLine, Coupon, ShippingRate,
+ ProductCategory, Product, ProductVariant, OrderLine, Coupon, ShippingRate,
SiteSettings
)
from core.usps import USPSApi
@@ -60,11 +60,11 @@ class CartItem:
class Cart:
item_class = CartItem
- # site_settings = SiteSettings.load()
def __init__(self, request):
self.request = request
self.session = request.session
+ self.site_settings = SiteSettings.load()
self.coupon_code = self.session.get('coupon_code')
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
@@ -129,6 +129,16 @@ class Cart:
def __len__(self):
return sum([item['quantity'] for item in self.cart])
+ def get_item_prices_for_category(self, category):
+ for item in self:
+ if item['variant'].product.category == category:
+ yield item['price_total']
+ else:
+ continue
+
+ def get_total_price_for_category(self, category):
+ return sum(self.get_item_prices_for_category(category))
+
def get_all_item_quantities(self):
for item in self.cart:
yield item['quantity']
@@ -139,8 +149,6 @@ class Cart:
def get_item_prices(self):
for item in self:
yield item['price_total']
- # for item in self.cart.values():
- # yield Decimal(item['price']) * sum([value['quantity'] for value in item['variations'].values()])
def get_total_price(self):
return sum(self.get_item_prices())
@@ -170,9 +178,11 @@ class Cart:
return containers
def get_shipping_cost(self, container=None):
- # free_shipping_min = self.site_settings.free_shipping_min
- # if self.get_total_price() >= free_shipping_min:
- # return Decimal('0.00')
+ free_shipping_min = self.site_settings.free_shipping_min
+ if free_shipping_min is not None:
+ category = ProductCategory.objects.get(name='Coffee')
+ if self.get_total_price_for_category(category) >= free_shipping_min:
+ return Decimal('0.00')
if container is None:
container = self.session.get('shipping_container').container
diff --git a/src/storefront/templates/storefront/cart_detail.html b/src/storefront/templates/storefront/cart_detail.html
index 6b34eb6..c7225b8 100644
--- a/src/storefront/templates/storefront/cart_detail.html
+++ b/src/storefront/templates/storefront/cart_detail.html
@@ -58,7 +58,10 @@
-
+
+ Free shipping on coffee orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}
+ *Merch does not count toward total
+
diff --git a/src/storefront/templates/storefront/order_form.html b/src/storefront/templates/storefront/order_form.html
index 03d8c0e..9447aa8 100644
--- a/src/storefront/templates/storefront/order_form.html
+++ b/src/storefront/templates/storefront/order_form.html
@@ -65,6 +65,10 @@
{% csrf_token %}
{{form.as_p}}
+
+ Free shipping on coffee orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}
+ Merchandise does not count toward total
+
diff --git a/src/storefront/tests/__init__.py b/src/storefront/tests/__init__.py
index e7723de..beb58bb 100644
--- a/src/storefront/tests/__init__.py
+++ b/src/storefront/tests/__init__.py
@@ -20,12 +20,12 @@ class RequestFaker:
},
},
'items': [{
- 'name': 'Decaf: 16 oz',
+ 'name': 'Decaf: 16 oz Grind: Whole Beans',
'description': 'Medium Roast',
'unit_amount': {'currency_code': 'USD', 'value': '13.40'},
'quantity': '1'
}, {
- 'name': 'Decaf: 16 oz',
+ 'name': 'Decaf: 16 oz Grind: Percolator',
'description': 'Medium Roast',
'unit_amount': {'currency_code': 'USD', 'value': '13.40'},
'quantity': '2'
diff --git a/src/storefront/tests/test_payments.py b/src/storefront/tests/test_payments.py
index e8ff9b6..146c9fc 100644
--- a/src/storefront/tests/test_payments.py
+++ b/src/storefront/tests/test_payments.py
@@ -36,7 +36,7 @@ class CreateOrderTest(TestCase):
name='16 oz',
sku='234987',
price=13.4,
- weight=Weight(oz=16),
+ weight=Weight(oz=16)
)
def setUp(self):