save handling option in frontend and recalculate handling when shop is

switched.

	modified:   templates/cart.j2
	modified:   views/cart.py
	modified:   views/shop_location.py
This commit is contained in:
Russell Ballestrini 2024-06-16 15:49:45 -04:00
parent 60b495b36a
commit 660e6d7ed6
3 changed files with 25 additions and 5 deletions

View file

@ -135,19 +135,19 @@
<h3>Physical Handling Options</h3>
<form action="{{ request.route_url('cart_handling_option', cart_id=cart.id) }}" method="POST">
{% if request.shop_location.local_pickup %}
<input type="radio" id="local_pickup" name="handling_option" value="local_pickup" required>
<input type="radio" id="local_pickup" name="handling_option" value="local_pickup" {% if cart.handling_option == "local_pickup" %}checked{% endif %} required>
<label for="local_pickup" style="display: inline;">Local Pickup (Free)</label><br>
{% endif %}
{% if request.shop_location.local_delivery %}
<input type="radio" id="local_delivery" name="handling_option" value="local_delivery" required>
<input type="radio" id="local_delivery" name="handling_option" value="local_delivery" {% if cart.handling_option == "local_delivery" %}checked{% endif %} required>
<label for="local_delivery" style="display: inline;">Local Delivery (${{ '%0.2f' % request.shop_location.local_delivery_rate }})</label><br>
{% endif %}
{% if request.shop_location.local_shipping %}
<input type="radio" id="local_shipping" name="handling_option" value="local_shipping" required>
<input type="radio" id="local_shipping" name="handling_option" value="local_shipping" {% if cart.handling_option == "local_shipping" %}checked{% endif %} required>
<label for="local_shipping" style="display: inline;">Local Shipping (${{ '%0.2f' % request.shop_location.local_shipping_rate }})</label><br>
{% endif %}
{% if request.shop_location.international_shipping %}
<input type="radio" id="international_shipping" name="handling_option" value="international_shipping" required>
<input type="radio" id="international_shipping" name="handling_option" value="international_shipping" {% if cart.handling_option == "international_shipping" %}checked{% endif %} required>
<label for="international_shipping" style="display: inline;">International Shipping (${{ '%0.2f' % request.shop_location.international_shipping_rate }})</label><br>
{% endif %}
<input type="submit" value="Set Handling Option">

View file

@ -369,7 +369,7 @@ def cart_handling_option(request):
request.dbsession.flush()
request.session.flash(("Handling option set successfully.", "success"))
return HTTPFound("/u/cart/{}".format(cart.id))
return HTTPFound(f"/cart/{cart.id}")
@view_config(route_name="cart_checkout", renderer="cart_checkout.j2")
@ -407,6 +407,11 @@ 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

@ -196,5 +196,20 @@ def shop_location_switch(request):
(f"You are now viewing the {location.name} shop location", "success")
)
# 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()
# Redirect to the next URL or the shop home page
return HTTPFound(next_url)