Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Create order transaction insufficient stock error
Anna Siwik0d77d2fe15d3
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.pyindex 67eee844eb..a6bad94c79 100644--- a/saleor/checkout/views/summary.py+++ b/saleor/checkout/views/summary.py@@ -2,6 +2,7 @@ from django.contrib import messagesfrom django.shortcuts import redirectfrom django.template.response import TemplateResponsefrom django.utils.translation import pgettext, pgettext_lazy+from satchless.item import InsufficientStockfrom ..forms import (AnonymousUserBillingForm, BillingAddressesForm,@@ -18,7 +19,10 @@ def create_order(checkout):`checkout` is a `saleor.checkout.core.Checkout` instance."""- order = checkout.create_order()+ try:+ order = checkout.create_order()+ except InsufficientStock:+ order = Noneif not order:return None, redirect('checkout:summary')checkout.clear_storage()diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 6e4dbd0d85..dcf40764e2 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -12,10 +12,10 @@ from django_prices.templatetags.prices_i18n import grossfrom payments import PaymentStatusfrom prices import Price-from .forms import (CancelGroupForm, CancelItemsForm, CancelOrderForm,- CapturePaymentForm, ChangeQuantityForm, MoveItemsForm,- OrderNoteForm, RefundPaymentForm, ReleasePaymentForm,- RemoveVoucherForm, ShipGroupForm)+from .forms import (+ CancelGroupForm, CancelItemsForm, CancelOrderForm, CapturePaymentForm,+ ChangeStockForm, ChangeQuantityForm, MoveItemsForm, OrderNoteForm,+ RefundPaymentForm, ReleasePaymentForm, RemoveVoucherForm, ShipGroupForm)from .utils import (create_packing_slip_pdf, create_invoice_pdf,get_statics_absolute_url)from ..order.forms import OrderFilterFormdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 0591d5db69..b32b7e724a 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -5,6 +5,7 @@ from django.dispatch import receiverfrom django.shortcuts import get_object_or_404, redirectfrom django.utils.translation import pgettext_lazyfrom payments.signals import status_changed+from satchless.item import InsufficientStockfrom ..core import analyticsfrom ..product.models import Stock@@ -60,9 +61,11 @@ def add_items_to_delivery_group(delivery_group, partition, discounts=None):while total_quantity > 0:stock = product_variant.select_stockrecord()+ if not stock:+ raise InsufficientStock(product_variant)quantity = (stock.quantity_available- if stock and total_quantity > stock.quantity_available+ if total_quantity > stock.quantity_availableelse total_quantity)delivery_group.items.create(diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex fb3d04c90e..99adae0689 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -3,6 +3,7 @@ from django.conf import settingsfrom django.contrib.auth.models import AnonymousUserfrom mock import MagicMock, Mockfrom prices import Price+from satchless.item import InsufficientStockfrom saleor.checkout import viewsfrom saleor.checkout.core import STORAGE_SESSION_KEY, Checkout@@ -201,3 +202,18 @@ def test_checkout_discount(request_cart, sale, product_in_stock):request_cart.add(variant, 1)checkout = Checkout(request_cart, AnonymousUser(), 'tracking_code')assert checkout.get_total() == Price(currency="USD", net=5)+++def test_checkout_create_order_insufficient_stock(+ request_cart, customer_user, product_in_stock, billing_address,+ shipping_method):+ product_class = product_in_stock.product_class+ product_class.is_shipping_required = False+ product_class.save()+ customer_user.default_billing_address = billing_address+ customer_user.save()+ variant = product_in_stock.variants.get()+ request_cart.add(variant, quantity=10, check_quantity=False)+ checkout = Checkout(request_cart, customer_user, 'tracking_code')+ with pytest.raises(InsufficientStock):+ checkout.create_order()