Skip to content

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

Allow to add product variant with 0 price to draft order

IKarbowiak9ad095549c08
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 6c2d92eef0..ec4da8367a 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -663,6 +663,68 @@ def test_draft_order_create(
assert created_draft_event.parameters == {}
+def test_draft_order_create_variant_with_0_price(
+ staff_api_client,
+ permission_manage_orders,
+ staff_user,
+ customer_user,
+ product_without_shipping,
+ shipping_method,
+ variant,
+ voucher,
+ graphql_address_data,
+):
+ variant_0 = variant
+ query = DRAFT_ORDER_CREATE_MUTATION
+ # Ensure no events were created yet
+ assert not OrderEvent.objects.exists()
+
+ user_id = graphene.Node.to_global_id("User", customer_user.id)
+ variant_0_id = graphene.Node.to_global_id("ProductVariant", variant_0.id)
+ variant_1 = product_without_shipping.variants.first()
+ variant_1.quantity = 2
+ variant.price = Money(0, "USD")
+ variant_1.save()
+ variant_1_id = graphene.Node.to_global_id("ProductVariant", variant_1.id)
+ variant_list = [
+ {"variantId": variant_0_id, "quantity": 2},
+ {"variantId": variant_1_id, "quantity": 1},
+ ]
+ shipping_address = graphql_address_data
+ shipping_id = graphene.Node.to_global_id("ShippingMethod", shipping_method.id)
+ variables = {
+ "user": user_id,
+ "lines": variant_list,
+ "shippingAddress": shipping_address,
+ "shippingMethod": shipping_id,
+ }
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_orders]
+ )
+ content = get_graphql_content(response)
+ assert not content["data"]["draftOrderCreate"]["orderErrors"]
+ data = content["data"]["draftOrderCreate"]["order"]
+ assert data["status"] == OrderStatus.DRAFT.upper()
+
+ order = Order.objects.first()
+ assert order.user == customer_user
+ # billing address should be copied
+ assert order.billing_address.pk != customer_user.default_billing_address.pk
+ assert (
+ order.billing_address.as_data()
+ == customer_user.default_billing_address.as_data()
+ )
+ assert order.shipping_method == shipping_method
+ assert order.shipping_address.first_name == graphql_address_data["firstName"]
+
+ # Ensure the correct event was created
+ created_draft_event = OrderEvent.objects.get(
+ type=order_events.OrderEvents.DRAFT_CREATED
+ )
+ assert created_draft_event.user == staff_user
+ assert created_draft_event.parameters == {}
+
+
@patch("saleor.graphql.order.mutations.draft_orders.add_variant_to_draft_order")
def test_draft_order_create_tax_error(
add_variant_to_draft_order_mock,
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index 686eae0e26..b0394b70c5 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -70,6 +70,28 @@ def test_add_variant_to_draft_order_adds_line_for_new_variant(
assert line.product_name == str(variant.product)
+def test_add_variant_to_draft_order_adds_line_for_variant_with_price_0(
+ order_with_lines, product, product_translation_fr, settings
+):
+ order = order_with_lines
+ variant = product.variants.get()
+ variant.price = Money(0, "USD")
+ variant.save(update_fields=["price"])
+
+ lines_before = order.lines.count()
+ settings.LANGUAGE_CODE = "fr"
+ add_variant_to_draft_order(order, variant, 1)
+
+ line = order.lines.last()
+ assert order.lines.count() == lines_before + 1
+ assert line.product_sku == variant.sku
+ assert line.quantity == 1
+ assert line.unit_price == TaxedMoney(net=Money(0, "USD"), gross=Money(0, "USD"))
+ assert line.translated_product_name == str(variant.product.translated)
+ assert line.variant_name == str(variant)
+ assert line.product_name == str(variant.product)
+
+
def test_add_variant_to_draft_order_not_allocates_stock_for_new_variant(
order_with_lines, product
):
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 9fe52fa42a..6be285e8a2 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -204,7 +204,9 @@ def add_variant_to_draft_order(order, variant, quantity, discounts=None):
manager = get_plugins_manager()
unit_price = manager.calculate_order_line_unit(line)
line.unit_price = unit_price
- line.tax_rate = unit_price.tax / unit_price.net
+ line.tax_rate = (
+ unit_price.tax / unit_price.net if unit_price.net.amount != 0 else 0
+ )
line.save(
update_fields=[
"currency",