Add free shipping only to coffee

This commit is contained in:
Nathan Chapman 2022-11-13 12:38:38 -07:00
parent f52b529c86
commit 94908d4230
6 changed files with 29 additions and 12 deletions

View File

@ -10,7 +10,7 @@
{% csrf_token %}
{{form.as_p}}
<p class="form__submit">
<input class="action-button" type="submit" value="Create category"> or <a href="{% url 'dashboard:category-detail' category.pk %}">cancel</a>
<input class="action-button" type="submit" value="Save changes"> or <a href="{% url 'dashboard:category-detail' category.pk %}">cancel</a>
</p>
</form>
</section>

View File

@ -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

View File

@ -58,7 +58,10 @@
<input type="submit" value="Apply" class="action-button">
</p>
</form>
<!-- <h5>Free shipping on orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}</h5> -->
<h5>
Free shipping on coffee orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}<br>
<small>*Merch does not count toward total</small>
</h5>
<div class="cart__table-wrapper">
<table class="cart__totals">
<tr>

View File

@ -65,6 +65,10 @@
{% csrf_token %}
{{form.as_p}}
</form>
<h5>
Free shipping on coffee orders over ${{ site_settings.free_shipping_min|floatformat:"2" }}<br>
<small><em>Merchandise does not count toward total</em></small>
</h5>
<div class="cart__table-wrapper">
<table class="cart__totals">
<tr>

View File

@ -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'

View File

@ -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):