Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use lines instead of groups in order
Anna Siwik09e7a5a39a41
diff --git a/saleor/account/views.py b/saleor/account/views.pyindex 7ed4dfdba2..9bbb968fcd 100755--- a/saleor/account/views.py+++ b/saleor/account/views.py@@ -74,7 +74,7 @@ def password_reset_confirm(request, uidb64=None, token=None):@login_requireddef details(request):password_form = get_or_process_password_form(request)- orders = request.user.orders.prefetch_related('groups__lines')+ orders = request.user.orders.prefetch_related('lines')orders_paginated = get_paginator_items(orders, settings.PAGINATE_BY, request.GET.get('page'))ctx = {'addresses': request.user.addresses.all(),diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex e6eaa4e698..4aee07cf60 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -19,6 +19,7 @@ from ..discount.models import NotApplicable, Voucherfrom ..discount.utils import (get_voucher_discount_for_checkout, increase_voucher_usage)from ..order.models import Order+from ..order.utils import add_variant_to_orderfrom ..shipping.models import ANY_COUNTRY, ShippingMethodCountrySTORAGE_SESSION_KEY = 'checkout_storage'@@ -343,15 +344,9 @@ class Checkout:order = Order.objects.create(**order_data)- for partition in self.cart.partition():- shipping_required = partition.is_shipping_required()- shipping_method_name = (- smart_text(self.shipping_method) if shipping_required- else None)- group = order.groups.create(- shipping_method_name=shipping_method_name)- group.process(partition, self.cart.discounts)- group.save()+ for line in self.cart.lines.all():+ add_variant_to_order(+ order, line.variant, line.quantity, self.cart.discounts)if voucher is not None:increase_voucher_usage(voucher)diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 5ad1b75480..b3e6de40f0 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -197,7 +197,7 @@ class ChangeQuantityForm(forms.ModelForm):class ShipGroupForm(forms.ModelForm):- """Dispatch a group and optionally assigns a tracking number.+ """Dispatch a group and optionally assign a tracking number.This process involves permanently decreasing the previously allocatedstock.diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 9980229023..c7693d2d6d 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -30,8 +30,8 @@ from .utils import (@staff_member_required@permission_required('order.view_order')def order_list(request):- orders = (Order.objects.prefetch_related(- 'payments', 'groups__lines', 'user').order_by('-pk'))+ orders = Order.objects.prefetch_related(+ 'payments', 'lines', 'user').order_by('-pk')order_filter = OrderFilter(request.GET, queryset=orders)orders = get_paginator_items(order_filter.qs, settings.DASHBOARD_PAGINATE_BY,@@ -45,9 +45,9 @@ def order_list(request):@staff_member_required@permission_required('order.view_order')def order_details(request, order_pk):- qs = (Order.objects- .select_related('user', 'shipping_address', 'billing_address')- .prefetch_related('notes', 'payments', 'history', 'groups__lines'))+ qs = Order.objects.select_related(+ 'user', 'shipping_address', 'billing_address').prefetch_related(+ 'notes', 'payments', 'history', 'lines')order = get_object_or_404(qs, pk=order_pk)notes = order.notes.all()all_payments = order.payments.exclude(status=PaymentStatus.INPUT)diff --git a/saleor/dashboard/views.py b/saleor/dashboard/views.pyindex a7b1347529..5e289ebdfa 100644--- a/saleor/dashboard/views.py+++ b/saleor/dashboard/views.py@@ -34,7 +34,7 @@ def superuser_required(def index(request):paginate_by = 10orders_to_ship = Order.objects.open().select_related(- 'user').prefetch_related('groups', 'groups__lines', 'payments')+ 'user').prefetch_related('lines', 'payments')orders_to_ship = [order for order in orders_to_ship if order.is_fully_paid()]payments = Payment.objects.filter(diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 440c8d0b90..1bd18a698c 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -116,7 +116,7 @@ class Order(models.Model):return self.shipping_address.phonedef __iter__(self):- return iter(self.groups.all())+ return iter(self.lines.all())def __repr__(self):return '<Order #%r>' % (self.id,)@@ -143,7 +143,7 @@ class Order(models.Model):return self.payments.filter(status=PaymentStatus.PREAUTH).exists()def is_shipping_required(self):- return any(group.is_shipping_required() for group in self.groups.all())+ return any(line.is_shipping_required for line in self)@propertydef status(self):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 5bd7f09866..22c4648c2c 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -46,14 +46,12 @@ def recalculate_order(order):Total price is a sum of items in shipment groups and order shipping priceminus discount amount."""- prices = [- group.get_total() for group in order- if group.status != GroupStatus.CANCELLED]+ prices = [line.get_total() for line in order]total = sum(prices, order.shipping_price)if order.discount_amount:total -= order.discount_amountorder.total = total- order.save()+ order.save(update_fields=['total'])def attach_order_to_user(order, user):@@ -106,6 +104,45 @@ def add_variant_to_delivery_group(quantity_left -= quantity+def add_variant_to_order(order, variant, total_quantity, discounts=None):+ """Add total_quantity of variant to group.++ Raises InsufficientStock exception if quantity could not be fulfilled.++ By default, first adds variant to existing lines with same variant.+ It can be disabled with setting add_to_existing to False.++ Order lines are created by increasing quantity of lines,+ as long as total_quantity of variant will be added.+ """+ quantity_left = total_quantity+ price = variant.get_price_per_item(discounts)+ while quantity_left > 0:+ stock = variant.select_stockrecord()+ if not stock:+ raise InsufficientStock(variant)+ quantity = (+ stock.quantity_available+ if quantity_left > stock.quantity_available+ else quantity_left+ )+ order.lines.create(+ product=variant.product,+ product_name=variant.display_product(),+ product_sku=variant.sku,+ is_shipping_required=(+ variant.product.product_type.is_shipping_required),+ quantity=quantity,+ unit_price_net=price.net,+ unit_price_gross=price.gross,+ stock=stock,+ stock_location=stock.location.name)+ allocate_stock(stock, quantity)+ # refresh stock for accessing quantity_allocated+ stock.refresh_from_db()+ quantity_left -= quantity++def add_variant_to_existing_lines(group, variant, total_quantity):"""Add variant to existing lines with same variant.diff --git a/saleor/order/views.py b/saleor/order/views.pyindex 20a7b48641..6a55b6206e 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -23,7 +23,7 @@ logger = logging.getLogger(__name__)def details(request, token):- orders = Order.objects.prefetch_related('groups__lines__product')+ orders = Order.objects.prefetch_related('lines__product')orders = orders.select_related('billing_address', 'shipping_address', 'user')order = get_object_or_404(orders, token=token)@@ -43,7 +43,7 @@ def details(request, token):def payment(request, token):- orders = Order.objects.prefetch_related('groups__lines__product')+ orders = Order.objects.prefetch_related('lines__product')orders = orders.select_related('billing_address', 'shipping_address', 'user')order = get_object_or_404(orders, token=token)diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.htmlindex 0642778cc9..33962ebb85 100644--- a/templates/order/_ordered_items_table.html+++ b/templates/order/_ordered_items_table.html@@ -46,54 +46,32 @@</div></div>-{% for group in order %}- {% for line in group %}- <div class="table__row order-details__product{% if forloop.last %} order-details__last-row{% endif %}">- <div class="row">- <div class="col-md-6 col-9">- <a class="link--clean" href="{% if line.product %}{{ line.product.get_absolute_url }}{% endif %}">- <img data-src="{% product_first_image line.product size="60x60" method="crop" %}"- data-srcset="{% product_first_image line.product size="60x60" method="crop" %} 1x, {% product_first_image line.product size="120x120" method="crop" %} 2x"- class="float-left lazyload lazypreload"- src="{% placeholder size=60 %}">- <span class="order-details__product__description">{{ line.product_name }}</span>- </a>- </div>- <div class="col-md-6 col-3">- <div class="row">- <div class="col-md-6 col-12">- <p class="float-right">x {{ line.quantity }}</p>- </div>- <div class="col-md-6 col-12">- <p class="float-right">{{ line.get_total.gross|amount:'html' }}</p>- </div>+{% for line in order %}+ <div class="table__row order-details__product{% if forloop.last %} order-details__last-row{% endif %}">+ <div class="row">+ <div class="col-md-6 col-9">+ <a class="link--clean" href="{% if line.product %}{{ line.product.get_absolute_url }}{% endif %}">+ <img data-src="{% product_first_image line.product size="60x60" method="crop" %}"+ data-srcset="{% product_first_image line.product size="60x60" method="crop" %} 1x, {% product_first_image line.product size="120x120" method="crop" %} 2x"+ class="float-left lazyload lazypreload"+ src="{% placeholder size=60 %}">+ <span class="order-details__product__description">{{ line.product_name }}</span>+ </a>+ </div>+ <div class="col-md-6 col-3">+ <div class="row">+ <div class="col-md-6 col-12">+ <p class="float-right">x {{ line.quantity }}</p>+ </div>+ <div class="col-md-6 col-12">+ <p class="float-right">{{ line.get_total.gross|amount:'html' }}</p>+ </div></div></div></div></div>- {% endfor %}- {% if order.is_shipping_required %}- <div class="table__row">- <div class="row">- <div class="col-12 order-details__table-footer">- <p>- {% blocktrans trimmed with shipping_method_name=group.shipping_method_name context "Order details shipment row text" %}- Shipping ({{ shipping_method_name }})- {% endblocktrans %}- </p>- {% if group.tracking_number %}- <p>- {% blocktrans trimmed with tracking_number=group.tracking_number context "Order details shipment row text" %}- Tracking number: {{ tracking_number }}- {% endblocktrans %}- </p>- {% endif %}- </div>- </div></div>-{% endif %}{% endfor %}-{% if order.is_shipping_required %}<div class="table__row"><div class="row">