refactor to make sure we ask user for address if physical products in

cart.

	modified:   make_post_sell/models/cart.py
	modified:   make_post_sell/views/cart.py
	modified:   make_post_sell/views/shop_location.py
This commit is contained in:
Russell Ballestrini 2024-06-16 17:59:21 -04:00
parent 660e6d7ed6
commit 03e61e9863
3 changed files with 27 additions and 30 deletions

View file

@ -276,7 +276,7 @@ class Cart(RBase, Base):
self._discounted_shop_totals_in_cents = deepcopy(self.shop_totals_in_cents)
if len(self.coupons) > 0:
for coupon in sorted(self.coupons):
for coupon in sorted(self.coupons, key=lambda c: c.id):
for (
shop_uuid,
shop_total_in_cents,
@ -444,6 +444,20 @@ class Cart(RBase, Base):
self.handling_option = None
self.handling_cost_in_cents = 0
def update_handling_cost(self, shop_location):
"""
Update the handling cost based on the provided shop location.
"""
if self.handling_option:
if self.handling_option == "local_pickup":
self.handling_cost_in_cents = 0
elif self.handling_option == "local_delivery":
self.handling_cost_in_cents = shop_location.local_delivery_rate_in_cents
elif self.handling_option == "local_shipping":
self.handling_cost_in_cents = shop_location.local_shipping_rate_in_cents
elif self.handling_option == "international_shipping":
self.handling_cost_in_cents = shop_location.international_shipping_rate_in_cents
def get_all_carts(dbsession):
"""

View file

@ -338,7 +338,6 @@ def cart_quantity_product(request):
@view_config(route_name="cart_handling_option", request_method="POST")
@user_required()
def cart_handling_option(request):
cart = get_cart_from_matchdict(request)
handling_option = request.params.get("handling_option")
@ -353,17 +352,7 @@ def cart_handling_option(request):
return HTTPFound(get_referer_or_home(request))
cart.handling_option = handling_option
if handling_option == "local_pickup":
cart.handling_cost_in_cents = 0
elif handling_option == "local_delivery":
cart.handling_cost_in_cents = request.shop_location.local_delivery_rate_in_cents
elif handling_option == "local_shipping":
cart.handling_cost_in_cents = request.shop_location.local_shipping_rate_in_cents
elif handling_option == "international_shipping":
cart.handling_cost_in_cents = (
request.shop_location.international_shipping_rate_in_cents
)
cart.update_handling_cost(request.shop_location)
request.dbsession.add(cart)
request.dbsession.flush()
@ -397,6 +386,14 @@ def cart_checkout(request):
elif cart.is_empty:
msg = ("That cart is empty, you cannot checkout.", "error")
elif cart.physical_products and not cart.handling_option:
msg = ("Please select a handling option for your physical items.", "error")
elif cart.physical_products and not request.user.active_address:
msg = ("Please add a shipping address to proceed with checkout.", "info")
request.session.flash(msg)
return HTTPFound("/u/addresses")
elif cart.requires_payment and stripe_user_shop is None:
msg = ("Please enter your payment information.", "info")
request.session.flash(msg)
@ -407,11 +404,6 @@ def cart_checkout(request):
request.session.flash(msg)
return HTTPFound("/billing")
elif cart.physical_products and not cart.handling_option:
msg = ("Please select a handling option for your physical items.", "error")
request.session.flash(msg)
return HTTPFound(f"/cart/{cart.id}")
else:
# make sure all coupon terms are met before proceeding.
error_messages = cart.validate_attached_coupons()

View file

@ -198,18 +198,9 @@ def shop_location_switch(request):
# Recalculate the handling cost based on the new location
cart = request.active_cart
if cart.handling_option:
if cart.handling_option == "local_pickup":
cart.handling_cost_in_cents = 0
elif cart.handling_option == "local_delivery":
cart.handling_cost_in_cents = location.local_delivery_rate_in_cents
elif cart.handling_option == "local_shipping":
cart.handling_cost_in_cents = location.local_shipping_rate_in_cents
elif cart.handling_option == "international_shipping":
cart.handling_cost_in_cents = location.international_shipping_rate_in_cents
request.dbsession.add(cart)
request.dbsession.flush()
cart.update_handling_cost(location)
request.dbsession.add(cart)
request.dbsession.flush()
# Redirect to the next URL or the shop home page
return HTTPFound(next_url)