Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Remove shipping_method from checkout object

Anna Siwik757309f12d58
diff --git a/saleor/cart/checkout/utils.py b/saleor/cart/checkout/utils.py
index 9d16d46bb5..09051095da 100644
--- a/saleor/cart/checkout/utils.py
+++ b/saleor/cart/checkout/utils.py
@@ -22,7 +22,10 @@ def get_checkout_data(cart, discounts, taxes):
lines = [
(line, line.get_total(discounts, taxes)) for line in cart.lines.all()]
subtotal = cart.get_total(discounts, taxes)
+ shipping_price = cart.get_shipping_price(taxes)
return {
'cart': cart,
+ 'cart_are_taxes_handled': bool(taxes),
'cart_lines': lines,
- 'cart_subtotal': subtotal}
+ 'cart_subtotal': subtotal,
+ 'cart_shipping_price': shipping_price}
diff --git a/saleor/cart/checkout/views/__init__.py b/saleor/cart/checkout/views/__init__.py
index 0489f93c09..aa1dfeef3c 100644
--- a/saleor/cart/checkout/views/__init__.py
+++ b/saleor/cart/checkout/views/__init__.py
@@ -32,18 +32,16 @@ def shipping_address_view(request, cart, checkout):
def shipping_method_view(request, cart, checkout):
"""Display the shipping method selection step."""
taxes = checkout.get_taxes()
- shipping_method_form = CartShippingMethodForm(
+ form = CartShippingMethodForm(
request.POST or None, taxes=taxes, instance=cart,
initial={'shipping_method': cart.shipping_method})
- if shipping_method_form.is_valid():
- checkout.shipping_method = (
- shipping_method_form.cleaned_data['shipping_method'])
- shipping_method_form.save()
+ if form.is_valid():
+ form.save()
return redirect('checkout:summary')
ctx = get_checkout_data(cart, request.discounts, taxes)
ctx.update({
'checkout': checkout,
- 'shipping_method_form': shipping_method_form})
+ 'shipping_method_form': form})
return TemplateResponse(request, 'checkout/shipping_method.html', ctx)
diff --git a/saleor/cart/models.py b/saleor/cart/models.py
index 4c4efe9f54..b2f8e6531d 100644
--- a/saleor/cart/models.py
+++ b/saleor/cart/models.py
@@ -14,6 +14,7 @@ from prices import sum as sum_prices
from . import CartStatus, logger
from ..account.models import Address
+from ..core.utils.taxes import ZERO_TAXED_MONEY
from ..shipping.models import ShippingMethodCountry
CENTS = Decimal('0.01')
@@ -131,6 +132,12 @@ class Cart(models.Model):
def __len__(self):
return self.lines.count()
+ def get_shipping_price(self, taxes):
+ return (
+ self.shipping_method.get_total_price(taxes)
+ if self.shipping_method and self.is_shipping_required()
+ else ZERO_TAXED_MONEY)
+
def get_total(self, discounts=None, taxes=None):
"""Return the total cost of the cart prior to shipping."""
subtotals = [
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index cc3a43d185..72b3a0d1e3 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -13,12 +13,11 @@ from ..account.utils import store_user_address
from ..cart.models import Cart
from ..cart.utils import get_or_empty_db_cart
from ..core import analytics
-from ..core.utils.taxes import ZERO_TAXED_MONEY, get_taxes_for_country
+from ..core.utils.taxes import get_taxes_for_country
from ..discount.models import NotApplicable, Voucher
from ..discount.utils import increase_voucher_usage
from ..order.models import Order
from ..order.utils import add_variant_to_order
-from ..shipping.models import ANY_COUNTRY, ShippingMethodCountry
from .utils import get_voucher_discount_for_checkout
STORAGE_SESSION_KEY = 'checkout_storage'
@@ -48,7 +47,6 @@ class Checkout:
self.taxes = taxes
self.tracking_code = tracking_code
self.storage = {'version': self.VERSION}
- self._shipping_method = None
@classmethod
def from_storage(
@@ -96,46 +94,6 @@ class Checkout:
"""Return `True` if this checkout session needs shipping."""
return self.cart.is_shipping_required()
- @property
- def are_taxes_handled(self):
- """Return `True` if taxes are handled in the delivery country."""
- return bool(self.get_taxes())
-
- @property
- def shipping_method(self):
- """Return a shipping method if any."""
- if self._shipping_method is None:
- if not self.cart.shipping_address:
- return None
- shipping_method_country_id = self.storage.get(
- 'shipping_method_country_id')
- if shipping_method_country_id is None:
- return None
- try:
- shipping_method_country = ShippingMethodCountry.objects.get(
- id=shipping_method_country_id)
- except ShippingMethodCountry.DoesNotExist:
- return None
- shipping_country_code = self.cart.shipping_address.country.code
- allowed_codes = [ANY_COUNTRY, shipping_country_code]
- if shipping_method_country.country_code not in allowed_codes:
- return None
- self._shipping_method = shipping_method_country
- return self._shipping_method
-
- @shipping_method.setter
- def shipping_method(self, shipping_method_country):
- self.storage['shipping_method_country_id'] = shipping_method_country.id
- self.modified = True
- self._shipping_method = shipping_method_country
-
- @property
- def shipping_price(self):
- shipping_method = self.cart.shipping_method
- return (
- shipping_method.get_total_price(self.get_taxes())
- if shipping_method else ZERO_TAXED_MONEY)
-
@property
def email(self):
"""Return the customer email if any."""
@@ -253,6 +211,8 @@ class Checkout:
which language to use when sending email.
"""
# FIXME: save locale along with the language
+ taxes = self.get_taxes()
+
voucher = self._get_voucher(
vouchers=Voucher.objects.active(date=date.today())
.select_for_update())
@@ -285,9 +245,9 @@ class Checkout:
'billing_address': billing_address,
'shipping_address': shipping_address,
'tracking_client_id': self.tracking_code,
- 'shipping_price': self.shipping_price,
'shipping_method': shipping_method,
'shipping_method_name': shipping_method_name,
+ 'shipping_price': self.cart.get_shipping_price(taxes),
'total': self.get_total()}
if self.user.is_authenticated:
@@ -305,8 +265,8 @@ class Checkout:
for line in self.cart.lines.all():
add_variant_to_order(
- order, line.variant, line.quantity, self.discounts,
- self.get_taxes(), add_to_existing=False)
+ order, line.variant, line.quantity, self.discounts, taxes,
+ add_to_existing=False)
if voucher is not None:
increase_voucher_usage(voucher)
@@ -356,7 +316,7 @@ class Checkout:
"""Calculate order total with shipping and discount amount."""
total = self.get_subtotal()
if self.cart.shipping_method and self.is_shipping_required:
- total += self.shipping_price
+ total += self.cart.get_shipping_price(self.get_taxes())
if self.discount:
total -= self.discount
return total
diff --git a/templates/checkout/details.html b/templates/checkout/details.html
index 8a5076bb42..86e2b89f3f 100644
--- a/templates/checkout/details.html
+++ b/templates/checkout/details.html
@@ -63,7 +63,7 @@
<div class="col-4">
<p class="float-right">
{% if cart.shipping_method %}
- {% price checkout.shipping_price %}
+ {% price cart_shipping_price %}
{% else %}
&mdash;
{% endif %}
@@ -71,7 +71,7 @@
</div>
</div>
{% with total=checkout.get_total %}
- {% if checkout.are_taxes_handled %}
+ {% if cart_are_taxes_handled %}
<div class="row checkout__review__section">
<div class="col-8">
{% if site.settings.display_gross_prices %}