Add checks for shipping information
This commit is contained in:
parent
4da1c860c0
commit
7e92e233fe
@ -3,6 +3,8 @@ import logging
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.shortcuts import redirect, reverse
|
||||||
|
from django.urls import reverse_lazy
|
||||||
|
|
||||||
from core.models import Product, OrderLine, Coupon
|
from core.models import Product, OrderLine, Coupon
|
||||||
from core.usps import USPSApiWithRate
|
from core.usps import USPSApiWithRate
|
||||||
@ -22,6 +24,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class Cart:
|
class Cart:
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
|
self.request = request
|
||||||
self.session = request.session
|
self.session = request.session
|
||||||
self.coupon_code = self.session.get('coupon_code')
|
self.coupon_code = self.session.get('coupon_code')
|
||||||
cart = self.session.get(settings.CART_SESSION_ID)
|
cart = self.session.get(settings.CART_SESSION_ID)
|
||||||
@ -73,10 +76,12 @@ class Cart:
|
|||||||
return sum(item['quantity'] for item in self.cart.values())
|
return sum(item['quantity'] for item in self.cart.values())
|
||||||
|
|
||||||
def get_total_weight(self):
|
def get_total_weight(self):
|
||||||
|
if len(self) > 0:
|
||||||
return sum([item['product'].weight.value * item['quantity'] for item in self])
|
return sum([item['product'].weight.value * item['quantity'] for item in self])
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
def get_shipping_box(self):
|
def get_shipping_box(self):
|
||||||
logger.info(len(self))
|
|
||||||
if len(self) > 6 and len(self) <= 10:
|
if len(self) > 6 and len(self) <= 10:
|
||||||
return ShippingContainer.LG_FLAT_RATE_BOX
|
return ShippingContainer.LG_FLAT_RATE_BOX
|
||||||
elif len(self) > 2 and len(self) <= 6:
|
elif len(self) > 2 and len(self) <= 6:
|
||||||
@ -87,10 +92,19 @@ class Cart:
|
|||||||
return ShippingContainer.VARIABLE
|
return ShippingContainer.VARIABLE
|
||||||
|
|
||||||
def get_shipping_cost(self):
|
def get_shipping_cost(self):
|
||||||
|
if len(self) > 0:
|
||||||
usps_rate_request = self.build_usps_rate_request()
|
usps_rate_request = self.build_usps_rate_request()
|
||||||
usps = USPSApiWithRate(settings.USPS_USER_ID, test=True)
|
usps = USPSApiWithRate(settings.USPS_USER_ID, test=True)
|
||||||
validation = usps.get_rate(usps_rate_request)
|
validation = usps.get_rate(usps_rate_request)
|
||||||
return Decimal(validation.result['RateV4Response']['Package']['Postage']['CommercialRate'])
|
logger.info(validation.result)
|
||||||
|
try:
|
||||||
|
rate = Decimal(validation.result['RateV4Response']['Package']['Postage']['CommercialRate'])
|
||||||
|
return rate
|
||||||
|
except KeyError as e:
|
||||||
|
raise e("USPS Result has no 'Postage'")
|
||||||
|
return Decimal('0.00')
|
||||||
|
else:
|
||||||
|
return Decimal('0.00')
|
||||||
|
|
||||||
def get_total_price(self):
|
def get_total_price(self):
|
||||||
return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())
|
return sum(Decimal(item['price']) * item['quantity'] for item in self.cart.values())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user