Skip to content

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

get only available variants for checkout

Damian Wcfdf6d5abf91
diff --git a/saleor/checkout/error_codes.py b/saleor/checkout/error_codes.py
index 88edb54539..43aa04ff25 100644
--- a/saleor/checkout/error_codes.py
+++ b/saleor/checkout/error_codes.py
@@ -24,3 +24,4 @@ class CheckoutErrorCode(Enum):
ZERO_QUANTITY = "zero_quantity"
MISSING_CHANNEL_SLUG = "missing_channel_slug"
CHANNEL_INACTIVE = "channel_inactive"
+ UNAVAILABLE_VARIANT_IN_CHANNEL = "unavailable_variant_in_channel"
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 2c7e802469..b7d9bbbdd5 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -170,6 +170,28 @@ def validate_variants_available_for_purchase(variants, channel_id):
)
+def validate_variants_available_in_channel(variants, channel_id):
+ variants_id = {variant.id for variant in variants}
+ available_variants = product_models.ProductVariantChannelListing.objects.filter(
+ variant__in=variants_id, channel_id=channel_id, price_amount__isnull=False
+ ).values_list("variant_id", flat=True)
+ not_available_variants = variants_id - set(available_variants)
+ if not_available_variants:
+ not_available_variants_ids = {
+ graphene.Node.to_global_id("ProductVariant", pk)
+ for pk in not_available_variants
+ }
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ "Cannot add lines with unavailable variants.",
+ code=CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL,
+ params={"variants": not_available_variants_ids},
+ )
+ }
+ )
+
+
class CheckoutLineInput(graphene.InputObjectType):
quantity = graphene.Int(required=True, description="The number of items purchased.")
variant_id = graphene.ID(required=True, description="ID of the product variant.")
@@ -236,6 +258,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
quantities = [line["quantity"] for line in lines]
validate_variants_available_for_purchase(variants, channel_id)
+ validate_variants_available_in_channel(variants, channel_id)
check_lines_quantity(variants, quantities, country)
return variants, quantities
@@ -430,6 +453,7 @@ class CheckoutLinesAdd(BaseMutation):
check_lines_quantity(variants, quantities, checkout.get_country())
validate_variants_available_for_purchase(variants, checkout.channel_id)
+ validate_variants_available_in_channel(variants, checkout.channel_id)
if variants and quantities:
for variant, quantity in zip(variants, quantities):
@@ -873,6 +897,8 @@ class CheckoutComplete(BaseMutation):
raise e
lines = fetch_checkout_lines(checkout)
+ variants = [line.variant for line in lines]
+ validate_variants_available_in_channel(variants, checkout.channel)
checkout_info = fetch_checkout_info(checkout, lines, info.context.discounts)
order, action_required, action_data = complete_checkout(
manager=info.context.plugins,
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 4aede88c3c..a9f1495e1d 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -212,6 +212,34 @@ def test_checkout_create_with_inactive_channel(
assert error["code"] == CheckoutErrorCode.CHANNEL_INACTIVE.name
+def test_checkout_create_with_unavailable_variant(
+ api_client, stock, graphql_address_data, channel_USD
+):
+
+ variant = stock.product_variant
+ variant.channel_listings.filter(channel=channel_USD).delete()
+ variant.channel_listings.create(channel=channel_USD)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "channel": channel_USD.slug,
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+
+ response = api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ error = get_graphql_content(response)["data"]["checkoutCreate"]["checkoutErrors"][0]
+
+ assert error["field"] == "lines"
+ assert error["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert error["variants"] == [variant_id]
+
+
def test_checkout_create_with_inactive_default_channel(
api_client, stock, graphql_address_data, channel_USD
):
@@ -1347,6 +1375,31 @@ def test_checkout_lines_add(
mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+def test_checkout_lines_add_with_unavailable_variant(
+ user_api_client, checkout_with_item, stock
+):
+ variant = stock.product_variant
+ variant.channel_listings.filter(channel=checkout_with_item.channel).delete()
+ variant.channel_listings.create(channel=checkout_with_item.channel)
+ checkout = checkout_with_item
+ line = checkout.lines.first()
+ assert line.quantity == 3
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": [{"variantId": variant_id, "quantity": 1}],
+ "channelSlug": checkout.channel.slug,
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutLinesAdd"]["checkoutErrors"]
+ assert errors[0]["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["variants"] == [variant_id]
+
+
def test_checkout_lines_add_with_unpublished_product(
user_api_client, checkout_with_item, stock, channel_USD
):
@@ -1548,6 +1601,7 @@ MUTATION_CHECKOUT_LINES_UPDATE = """
field
code
message
+ variants
}
}
}
@@ -1590,6 +1644,33 @@ def test_checkout_lines_update(
mocked_update_shipping_method.assert_called_once_with(checkout_info, lines)
+def test_checkout_lines_update_with_unavailable_variant(
+ user_api_client, checkout_with_item
+):
+ checkout = checkout_with_item
+ assert checkout.lines.count() == 1
+ line = checkout.lines.first()
+ variant = line.variant
+ variant.channel_listings.filter(channel=checkout_with_item.channel).delete()
+ variant.channel_listings.create(channel=checkout_with_item.channel)
+ assert line.quantity == 3
+
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": [{"variantId": variant_id, "quantity": 1}],
+ }
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_UPDATE, variables)
+ content = get_graphql_content(response)
+
+ errors = content["data"]["checkoutLinesUpdate"]["checkoutErrors"]
+ assert errors[0]["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["variants"] == [variant_id]
+
+
def test_create_checkout_with_unpublished_product(
user_api_client, checkout_with_item, stock, channel_USD
):
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.py
index 450a010657..a1c3f12043 100644
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@ -30,6 +30,7 @@ MUTATION_CHECKOUT_COMPLETE = """
checkoutErrors {
field,
message,
+ variants,
code
}
confirmationNeeded
@@ -243,6 +244,44 @@ def test_checkout_complete(
order_confirmed_mock.assert_called_once_with(order)
+def test_checkout_complete_with_unavailable_variant(
+ site_settings,
+ user_api_client,
+ checkout_with_gift_card,
+ gift_card,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+
+ assert not gift_card.last_used_on
+
+ checkout = checkout_with_gift_card
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ 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_variant = checkout_line.variant
+ checkout_line_variant.channel_listings.filter(channel=checkout.channel).delete()
+ checkout_line_variant.channel_listings.create(channel=checkout.channel)
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", checkout_line_variant.pk)
+ redirect_url = "https://www.example.com"
+ variables = {"checkoutId": checkout_id, "redirectUrl": redirect_url}
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ content = get_graphql_content(response)
+ errors = content["data"]["checkoutComplete"]["checkoutErrors"]
+ assert errors[0]["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert errors[0]["field"] == "lines"
+ assert errors[0]["variants"] == [variant_id]
+
+
@patch("saleor.plugins.manager.PluginsManager.order_confirmed")
def test_checkout_complete_requires_confirmation(
order_confirmed_mock,
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 111844ad9b..708ed1d289 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -971,6 +971,7 @@ enum CheckoutErrorCode {
ZERO_QUANTITY
MISSING_CHANNEL_SLUG
CHANNEL_INACTIVE
+ UNAVAILABLE_VARIANT_IN_CHANNEL
}
type CheckoutLine implements Node {