Skip to content

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

Allow to finish chekcout when price amount is 0 (#6064)

Iga Karbowiak1424f17e4cbd
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 1b334e45dd..6f93d44355 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -614,11 +614,11 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLine
unit_price = quantize_price(
total_line_price / checkout_line.quantity, total_line_price.currency
)
- tax_rate = (
- unit_price.tax / unit_price.net
- if not isinstance(unit_price, Decimal)
- else Decimal("0.0")
- )
+ tax_rate = Decimal("0.0")
+ # The condition will return False when unit_price.gross is 0.0
+ if not isinstance(unit_price, Decimal) and unit_price.gross:
+ tax_rate = unit_price.tax / unit_price.net
+
line = OrderLine(
product_name=product_name,
variant_name=variant_name,
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 0b1a759ea3..4c15221098 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -1754,6 +1754,80 @@ def test_checkout_complete(
).exists(), "Checkout should have been deleted"
+def test_checkout_complete_0_total_value(
+ user_api_client,
+ checkout_with_item,
+ gift_card,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+
+ assert not gift_card.last_used_on
+
+ checkout = checkout_with_item
+ checkout.billing_address = address
+ checkout.store_value_in_metadata(items={"accepted": "true"})
+ checkout.store_value_in_private_metadata(items={"accepted": "false"})
+ checkout.save()
+
+ checkout_line = checkout.lines.first()
+ checkout_line_quantity = checkout_line.quantity
+ checkout_line_variant = checkout_line.variant
+
+ product_type = checkout_line_variant.product.product_type
+ product_type.is_shipping_required = False
+ product_type.save(update_fields=["is_shipping_required"])
+
+ checkout_line_variant.cost_price_amount = Decimal(0)
+ checkout_line_variant.price_amount = Decimal(0)
+ checkout_line_variant.save()
+
+ checkout.refresh_from_db()
+
+ total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ orders_count = Order.objects.count()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["checkoutErrors"]
+
+ order_token = data["order"]["token"]
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert order.token == order_token
+ assert order.total.gross == total.gross
+ assert order.metadata == checkout.metadata
+ assert order.private_metadata == checkout.private_metadata
+
+ order_line = order.lines.first()
+ assert checkout_line_quantity == order_line.quantity
+ assert checkout_line_variant == order_line.variant
+ assert order.shipping_address is None
+ assert order.shipping_method is None
+ assert order.payments.exists()
+ order_payment = order.payments.first()
+ assert order_payment == payment
+ assert payment.transactions.count() == 1
+
+ assert not Checkout.objects.filter(
+ pk=checkout.pk
+ ).exists(), "Checkout should have been deleted"
+
+
@pytest.mark.integration
def test_checkout_with_voucher_complete(
user_api_client,