Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplify adding variants to order
Anna Siwik5969d8985305
diff --git a/saleor/cart/checkout/utils.py b/saleor/cart/checkout/utils.pyindex fee866e64a..81c6c23ca8 100644--- a/saleor/cart/checkout/utils.py+++ b/saleor/cart/checkout/utils.py@@ -485,8 +485,7 @@ def create_order(cart, tracking_code, discounts, taxes):for line in cart.lines.all():add_variant_to_order(- order, line.variant, line.quantity, discounts, taxes,- add_to_existing=False)+ order, line.variant, line.quantity, discounts, taxes)if voucher:increase_voucher_usage(voucher)diff --git a/saleor/cart/utils.py b/saleor/cart/utils.pyindex de5941a5fb..febc3356f4 100644--- a/saleor/cart/utils.py+++ b/saleor/cart/utils.py@@ -132,7 +132,9 @@ def get_or_create_anonymous_cart_from_token(def get_or_create_user_cart(user, cart_queryset=Cart.objects.all()):"""Return an open cart for given user or create a new one."""- defaults = {'shipping_address': user.default_shipping_address}+ defaults = {+ 'shipping_address': user.default_shipping_address,+ 'billing_address': user.default_billing_address}return cart_queryset.open().get_or_create(user=user, defaults=defaults)[0]@@ -166,7 +168,10 @@ def get_cart_from_request(request, cart_queryset=Cart.objects.all()):if cart is not None:return cartif user:- return Cart(user=user, shipping_address=user.default_shipping_address)+ return Cart(+ user=user,+ shipping_address=user.default_shipping_address,+ billing_address=user.default_billing_address)return Cart()diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex e48971b975..c3ec7512ee 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -1,6 +1,5 @@from functools import wraps-from django.db.models import Ffrom django.shortcuts import get_object_or_404, redirectfrom ..account.utils import store_user_address@@ -137,7 +136,7 @@ def cancel_fulfillment(fulfillment, restock):def attach_order_to_user(order, user):- """Associates existing order with user account."""+ """Associate existing order with user account."""order.user = userstore_user_address(user, order.billing_address, billing=True)if order.shipping_address:@@ -145,78 +144,29 @@ def attach_order_to_user(order, user):order.save(update_fields=['user'])-def add_variant_to_order(- order, variant, total_quantity, discounts=None, taxes=None,- add_to_existing=True):+def add_variant_to_order(order, variant, quantity, discounts=None, taxes=None):"""Add total_quantity of variant to order.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_not_fulfilled = add_variant_to_existing_lines(- order, variant, total_quantity) if add_to_existing else total_quantity-- if not quantity_not_fulfilled:- return- if quantity_not_fulfilled > variant.quantity_available:+ if quantity > variant.quantity_available:raise InsufficientStock(variant)- order.lines.create(- product_name=variant.display_product(),- product_sku=variant.sku,- is_shipping_required=variant.is_shipping_required(),- quantity=quantity_not_fulfilled,- variant=variant,- unit_price=variant.get_price(discounts, taxes),- tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))- allocate_stock(variant, quantity_not_fulfilled)---def add_variant_to_existing_lines(order, variant, total_quantity):- """Add variant to existing lines with same variant.-- Variant is added by increasing quantity of lines with same variant,- as long as total_quantity of variant will be added- or there is no more lines with same variant.-- Returns quantity that could not be fulfilled with existing lines.- """- # order descending by lines' stock available quantity- lines = order.lines.filter(variant=variant).order_by(- F('variant__quantity_allocated') - F('variant__quantity'))-- quantity_left = total_quantity- for line in lines:- quantity = (- line.variant.quantity_available- if quantity_left > line.variant.quantity_available- else quantity_left)+ try:+ line = order.lines.get(variant=variant)line.quantity += quantity- line.save()- allocate_stock(line.variant, quantity)- quantity_left -= quantity- if quantity_left == 0:- break- return quantity_left---def merge_duplicates_into_order_line(line):- """Merge duplicated lines in order into one (given) line.-- If there are no duplicates, nothing will happen.- """- lines = line.order.lines.filter(- product_name=line.product_name, product_sku=line.product_sku,- variant=line.variant, is_shipping_required=line.is_shipping_required)- if lines.count() > 1:- line.quantity = sum([line.quantity for line in lines])line.save(update_fields=['quantity'])- lines.exclude(pk=line.pk).delete()+ except OrderLine.DoesNotExist:+ order.lines.create(+ product_name=variant.display_product(),+ product_sku=variant.sku,+ is_shipping_required=variant.is_shipping_required(),+ quantity=quantity,+ variant=variant,+ unit_price=variant.get_price(discounts, taxes),+ tax_rate=get_tax_rate_by_name(variant.product.tax_rate, taxes))++ allocate_stock(variant, quantity)def change_order_line_quantity(line, new_quantity):@@ -240,10 +190,9 @@ def restock_order_lines(order):line.save(update_fields=['quantity_fulfilled'])-def restock_fulfillment_lines(fulfillment, allocate=True):+def restock_fulfillment_lines(fulfillment):"""Return fulfilled products to corresponding stocks."""for line in fulfillment:- order_line = line.order_line- if order_line.variant:+ if line.order_line.variant:increase_stock(- line.order_line.variant, line.quantity, allocate=allocate)+ line.order_line.variant, line.quantity, allocate=True)diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex ce99a873d6..33dfed41ab 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -15,9 +15,7 @@ from saleor.dashboard.order.utils import (from saleor.discount.utils import increase_voucher_usagefrom saleor.order import OrderStatusfrom saleor.order.models import Order, OrderLine, OrderNote-from saleor.order.utils import (- add_variant_to_existing_lines, add_variant_to_order,- change_order_line_quantity)+from saleor.order.utils import add_variant_to_order, change_order_line_quantityfrom saleor.product.models import ProductVariant@@ -565,26 +563,6 @@ def test_view_fulfillment_packing_slips_without_shipping(assert response['content-type'] == 'application/pdf'-def test_add_variant_to_existing_lines_one_line(- order_with_lines_and_stock):- order = order_with_lines_and_stock- lines = order.lines.filter(product_sku='SKU_A')- variant_lines_before = lines.count()- line = lines.first()- line_quantity_before = line.quantity- variant = ProductVariant.objects.get(sku='SKU_A')-- quantity_added = 2- quantity_left = add_variant_to_existing_lines(- line.order, variant, quantity_added)-- lines_after = order.lines.filter(product_sku='SKU_A').count()- line.refresh_from_db()- assert quantity_left == 0- assert lines_after == variant_lines_before- assert line.quantity == line_quantity_before + quantity_added--def test_view_add_variant_to_order(admin_client, order_with_lines_and_stock):order = order_with_lines_and_stock