Update USPS shipping handling
This commit is contained in:
parent
b13511e59e
commit
f2003a711f
@ -115,17 +115,21 @@ class Cart:
|
||||
return ShippingContainer.VARIABLE
|
||||
|
||||
def get_shipping_cost(self):
|
||||
if len(self) > 0:
|
||||
usps_rate_request = self.build_usps_rate_request()
|
||||
if len(self) > 0 and self.session.get("shipping_address"):
|
||||
try:
|
||||
usps_rate_request = self.build_usps_rate_request()
|
||||
except TypeError as e:
|
||||
return Decimal('0.00')
|
||||
usps = USPSApiWithRate(settings.USPS_USER_ID, test=True)
|
||||
validation = usps.get_rate(usps_rate_request)
|
||||
logger.info(validation.result)
|
||||
try:
|
||||
rate = Decimal(validation.result['RateV4Response']['Package']['Postage']['CommercialRate'])
|
||||
return rate
|
||||
rate = validation.result['RateV4Response']['Package']['Postage']['CommercialRate']
|
||||
rate = '0.00'
|
||||
except KeyError as e:
|
||||
raise e("USPS Result has no 'Postage'")
|
||||
return Decimal('0.00')
|
||||
rate = '0.00'
|
||||
return Decimal(rate)
|
||||
else:
|
||||
return Decimal('0.00')
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
</figure>
|
||||
<div class="item__info">
|
||||
<h4>{{product.name}}</h4>
|
||||
<p><strong>Grind</strong>: {{item.grind}}</p>
|
||||
<p><strong>Grind</strong>:</p>
|
||||
{% for key, value in item.variations.items %}
|
||||
<p><strong>{{key}}</strong><br>
|
||||
<form class="item__form" action="{% url 'storefront:cart-update' product.pk key %}" method="POST">
|
||||
|
||||
@ -7,7 +7,7 @@ from django.shortcuts import render, reverse, redirect, get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import JsonResponse
|
||||
from django.http import JsonResponse, HttpResponseRedirect
|
||||
from django.views.generic.base import RedirectView, TemplateView
|
||||
from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView, FormMixin
|
||||
from django.views.generic.detail import DetailView, SingleObjectMixin
|
||||
@ -15,6 +15,7 @@ from django.views.generic.list import ListView
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.contrib import messages
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.forms.models import model_to_dict
|
||||
@ -193,13 +194,25 @@ class OrderCreateView(CreateView):
|
||||
form_class = OrderCreateForm
|
||||
success_url = reverse_lazy('storefront:payment-done')
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if not self.request.session.get("shipping_address"):
|
||||
messages.warning(request, 'Please add a shipping address.')
|
||||
return HttpResponseRedirect(reverse('storefront:checkout-address'))
|
||||
else:
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def get_initial(self):
|
||||
cart = Cart(self.request)
|
||||
try:
|
||||
shipping_cost = cart.get_shipping_cost()
|
||||
except Exception as e:
|
||||
raise e('Could not get shipping information')
|
||||
shipping_cost = Decimal('0.00')
|
||||
|
||||
initial = {
|
||||
'total_net_amount': cart.get_total_price(),
|
||||
'shipping_total': cart.get_shipping_cost()
|
||||
'shipping_total': shipping_cost
|
||||
}
|
||||
|
||||
if self.request.session.get('shipping_address'):
|
||||
a = self.request.session.get('shipping_address')
|
||||
user_info = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user