Skip to content

Instantly share code, notes, and snippets.

@fawx
Created May 3, 2015 23:19
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 fawx/e8a76bbacf206abd44b7 to your computer and use it in GitHub Desktop.
Save fawx/e8a76bbacf206abd44b7 to your computer and use it in GitHub Desktop.
def get_total_weight_in_oz(request, order_form):
# sort the shopping cart by skus so we can match it up with
# an actual queryset of product data (mostly for the weights)
cart_by_skus = sorted( request.cart , key=lambda item: item.sku )
skus = [item.sku for item in cart_by_skus]
quantities = [item.quantity for item in cart_by_skus]
products = ProductVariation.objects.filter(product__sku__in=skus).order_by('sku')
discount = ''
discounted_products = ''
if request.session.get('discount_code'):
discount = DiscountCode.objects.get(code=request.session.get('discount_code'))
if discount.free_shipping:
discounted_products = [product.sku for product in discount.all_products()]
i = 0
total_weight = 0
for product in products:
if product.product.sku not in discounted_products:
total_weight += quantities[i] * product.weight
i += 1
return total_weight * 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment