Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix pricing on product with missing channel listings (#6626)
Filip Owczarek27895293972f
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 36f6f1e8cc..b358d875d3 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -354,6 +354,11 @@ class ProductVariant(ChannelContextTypeWithMetadata, CountableDjangoObjectType):product_channel_listing,):def calculate_pricing_with_collections(collections):+ if (+ not variant_channel_listing+ or not product_channel_listing+ ):+ return Noneavailability = get_variant_availability(variant=root.node,variant_channel_listing=variant_channel_listing,@@ -639,6 +644,8 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):variants_channel_listing,):def calculate_pricing_with_collections(collections):+ if not variants_channel_listing:+ return Noneavailability = get_product_availability(product=root.node,product_channel_listing=product_channel_listing,diff --git a/saleor/product/tests/test_product_availability.py b/saleor/product/tests/test_product_availability.pyindex 701b2a7be0..5f9ead74ba 100644--- a/saleor/product/tests/test_product_availability.py+++ b/saleor/product/tests/test_product_availability.py@@ -1,4 +1,5 @@import datetime+from decimal import Decimalfrom unittest.mock import Mockfrom freezegun import freeze_time@@ -69,6 +70,91 @@ def test_availability(stock, monkeypatch, settings, channel_USD):assert availability.price_range_undiscounted.stop.tax.amount+def test_availability_with_all_variant_channel_listings(stock, channel_USD):+ # given+ product = stock.product_variant.product+ product_channel_listing = product.channel_listings.first()+ variants = product.variants.all()+ variants_channel_listing = models.ProductVariantChannelListing.objects.filter(+ variant__in=variants, channel=channel_USD+ )+ [variant1_channel_listing, variant2_channel_listing] = variants_channel_listing+ variant2_channel_listing.price_amount = Decimal(15)+ variant2_channel_listing.save()++ # when+ availability = get_product_availability(+ product=product,+ product_channel_listing=product_channel_listing,+ variants=variants,+ variants_channel_listing=variants_channel_listing,+ channel=channel_USD,+ collections=[],+ discounts=[],+ country="PL",+ )++ # then+ price_range = availability.price_range+ assert price_range.start.gross.amount == variant1_channel_listing.price_amount+ assert price_range.stop.gross.amount == variant2_channel_listing.price_amount+++def test_availability_with_missing_variant_channel_listings(stock, channel_USD):+ # given+ product = stock.product_variant.product+ product_channel_listing = product.channel_listings.first()+ variants = product.variants.all()+ variants_channel_listing = models.ProductVariantChannelListing.objects.filter(+ variant__in=variants, channel=channel_USD+ )+ [variant1_channel_listing, variant2_channel_listing] = variants_channel_listing+ variant2_channel_listing.delete()++ # when+ availability = get_product_availability(+ product=product,+ product_channel_listing=product_channel_listing,+ variants=variants,+ variants_channel_listing=variants_channel_listing,+ channel=channel_USD,+ collections=[],+ discounts=[],+ country="PL",+ )++ # then+ price_range = availability.price_range+ assert price_range.start.gross.amount == variant1_channel_listing.price_amount+ assert price_range.stop.gross.amount == variant1_channel_listing.price_amount+++def test_availability_without_variant_channel_listings(stock, channel_USD):+ # given+ product = stock.product_variant.product+ product_channel_listing = product.channel_listings.first()+ variants = product.variants.all()+ models.ProductVariantChannelListing.objects.filter(+ variant__in=variants, channel=channel_USD+ ).delete()++ # when+ availability = get_product_availability(+ product=product,+ product_channel_listing=product_channel_listing,+ variants=variants,+ variants_channel_listing=[],+ channel=channel_USD,+ collections=[],+ discounts=[],+ country="PL",+ )++ # then+ price_range = availability.price_range+ assert price_range is None++def test_available_products_only_published(product_list, channel_USD):channel_listing = product_list[0].channel_listings.get()channel_listing.is_published = Falsediff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.pyindex 86f2873e95..a26c4cd6f3 100644--- a/saleor/product/utils/availability.py+++ b/saleor/product/utils/availability.py@@ -113,18 +113,26 @@ def get_product_price_range() -> Optional[MoneyRange]:with opentracing.global_tracer().start_active_span("get_product_price_range"):if variants:- prices = [- get_variant_price(- variant=variant,- variant_channel_listing=variants_channel_listing[i],- product=product,- collections=collections,- discounts=discounts,- channel=channel,- )- for i, variant in enumerate(variants)- ]- return MoneyRange(min(prices), max(prices))+ variants_channel_listing_dict = {+ channel_listing.variant_id: channel_listing+ for channel_listing in variants_channel_listing+ if channel_listing+ }+ prices = []+ for variant in variants:+ variant_channel_listing = variants_channel_listing_dict.get(variant.id)+ if variant_channel_listing:+ price = get_variant_price(+ variant=variant,+ variant_channel_listing=variant_channel_listing,+ product=product,+ collections=collections,+ discounts=discounts,+ channel=channel,+ )+ prices.append(price)+ if prices:+ return MoneyRange(min(prices), max(prices))return None@@ -145,7 +153,6 @@ def get_product_availability(with opentracing.global_tracer().start_active_span("get_product_availability"):if not plugins:plugins = get_plugins_manager()-discounted = Nonediscounted_net_range = get_product_price_range(product=product,