Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add pricing to product channel listing (#6505)
Filip Owczarekb951fa91beb5
diff --git a/saleor/graphql/product/dataloaders/products.py b/saleor/graphql/product/dataloaders/products.pyindex 0574ba5f24..7f70b50fe6 100644--- a/saleor/graphql/product/dataloaders/products.py+++ b/saleor/graphql/product/dataloaders/products.py@@ -256,7 +256,9 @@ class VariantsChannelListingByProductIdAndChanneSlugLoader((product_id, channel_slug)] = variants_channel_listing- return [variant_channel_listing_by_product_and_channel[key] for key in keys]+ return [+ variant_channel_listing_by_product_and_channel.get(key, []) for key in keys+ ]def batch_load_channel(self, channel_slug: str, products_ids: Iterable[int]@@ -274,7 +276,7 @@ class VariantsChannelListingByProductIdAndChanneSlugLoader()return [- (products_id, variants_channel_listings_map.get(products_id))+ (products_id, variants_channel_listings_map.get(products_id, []))for products_id in products_ids]diff --git a/saleor/graphql/product/tests/benchmark/test_product.py b/saleor/graphql/product/tests/benchmark/test_product.pyindex 7cd2783ed5..9b7ad281d3 100644--- a/saleor/graphql/product/tests/benchmark/test_product.py+++ b/saleor/graphql/product/tests/benchmark/test_product.py@@ -228,6 +228,22 @@ def test_retrieve_channel_listings(}isAvailableForPurchaseavailableForPurchase+ pricing {+ priceRangeUndiscounted {+ start {+ gross {+ amount+ currency+ }+ }+ stop {+ gross {+ amount+ currency+ }+ }+ }+ }}}}diff --git a/saleor/graphql/product/tests/test_product_channel_listings_queries.py b/saleor/graphql/product/tests/test_product_channel_listings_queries.pynew file mode 100644index 0000000000..e30c5bcd66--- /dev/null+++ b/saleor/graphql/product/tests/test_product_channel_listings_queries.py@@ -0,0 +1,73 @@+import graphene++from ...tests.utils import get_graphql_content++QUERY_PRICING_ON_PRODUCT_CHANNEL_LISTING = """+query FetchProduct($id: ID, $channel: String) {+ product(id: $id, channel: $channel) {+ id+ pricing {+ priceRangeUndiscounted {+ start {+ gross {+ amount+ currency+ }+ }+ stop {+ gross {+ amount+ currency+ }+ }+ }+ }+ channelListings {+ channel {+ slug+ }+ pricing {+ priceRangeUndiscounted {+ start {+ gross {+ amount+ currency+ }+ }+ stop {+ gross {+ amount+ currency+ }+ }+ }+ }+ }+ }+}+"""+++def test_product_channel_listing_pricing_field(+ staff_api_client, permission_manage_products, channel_USD, product+):+ # given+ variables = {+ "id": graphene.Node.to_global_id("Product", product.pk),+ "channel": channel_USD.slug,+ }+ product.channel_listings.exclude(channel__slug=channel_USD.slug).delete()++ # when+ response = staff_api_client.post_graphql(+ QUERY_PRICING_ON_PRODUCT_CHANNEL_LISTING,+ variables=variables,+ permissions=(permission_manage_products,),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ product_data = content["data"]["product"]+ product_channel_listing_data = product_data["channelListings"][0]+ assert product_data["pricing"] == product_channel_listing_data["pricing"]diff --git a/saleor/graphql/product/types/channels.py b/saleor/graphql/product/types/channels.pyindex 34e92cf016..e1a84d671a 100644--- a/saleor/graphql/product/types/channels.py+++ b/saleor/graphql/product/types/channels.py@@ -1,8 +1,13 @@+from dataclasses import asdict+import graphene+from saleor.graphql.product.dataloaders.products import ProductByIdLoader+from ....core.permissions import ProductPermissionsfrom ....graphql.core.types import Money, MoneyRangefrom ....product import models+from ....product.utils.availability import get_product_availabilityfrom ....product.utils.costs import (get_margin_for_variant_channel_listing,get_product_costs_data,@@ -10,9 +15,12 @@ from ....product.utils.costs import (from ...channel.dataloaders import ChannelByIdLoaderfrom ...core.connection import CountableDjangoObjectTypefrom ...decorators import permission_required-from ...product.dataloaders import (+from ...discount.dataloaders import DiscountsByDateTimeLoader+from ..dataloaders import (+ CollectionsByProductIdLoader,ProductVariantsByProductIdLoader,VariantChannelListingByVariantIdAndChannelSlugLoader,+ VariantsChannelListingByProductIdAndChanneSlugLoader,)@@ -30,6 +38,13 @@ class ProductChannelListing(CountableDjangoObjectType):is_available_for_purchase = graphene.Boolean(description="Whether the product is available for purchase.")+ pricing = graphene.Field(+ "saleor.graphql.product.types.products.ProductPricingInfo",+ description=(+ "Lists the storefront product's pricing, the current price and discounts, "+ "only meant for displaying."+ ),+ )class Meta:description = "Represents product channel listing."@@ -126,6 +141,74 @@ class ProductChannelListing(CountableDjangoObjectType):def resolve_is_available_for_purchase(root: models.ProductChannelListing, _info):return root.is_available_for_purchase()+ @staticmethod+ def resolve_pricing(root: models.ProductChannelListing, info):+ context = info.context++ def calculate_pricing_info(discounts):+ def calculate_pricing_with_channel(channel):+ def calculate_pricing_with_product(product):+ def calculate_pricing_with_variants(variants):+ def calculate_pricing_with_variants_channel_listings(+ variants_channel_listing,+ ):+ def calculate_pricing_with_collections(collections):+ if not variants_channel_listing:+ return None+ availability = get_product_availability(+ product=product,+ product_channel_listing=root,+ variants=variants,+ variants_channel_listing=variants_channel_listing,+ collections=collections,+ discounts=discounts,+ channel=channel,+ country=context.country,+ local_currency=context.currency,+ plugins=context.plugins,+ )+ from .products import ProductPricingInfo++ return ProductPricingInfo(**asdict(availability))++ return (+ CollectionsByProductIdLoader(context)+ .load(root.product_id)+ .then(calculate_pricing_with_collections)+ )++ return (+ VariantsChannelListingByProductIdAndChanneSlugLoader(+ context+ )+ .load((root.product_id, channel.slug))+ .then(calculate_pricing_with_variants_channel_listings)+ )++ return (+ ProductVariantsByProductIdLoader(context)+ .load(root.product_id)+ .then(calculate_pricing_with_variants)+ )++ return (+ ProductByIdLoader(context)+ .load(root.product_id)+ .then(calculate_pricing_with_product)+ )++ return (+ ChannelByIdLoader(context)+ .load(root.channel_id)+ .then(calculate_pricing_with_channel)+ )++ return (+ DiscountsByDateTimeLoader(context)+ .load(info.context.request_time)+ .then(calculate_pricing_info)+ )+class ProductVariantChannelListing(CountableDjangoObjectType):cost_price = graphene.Field(Money, description="Cost price of the variant.")diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 9697cfd23e..7c3254ee98 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3603,6 +3603,7 @@ type ProductChannelListing implements Node {purchaseCost: MoneyRangemargin: MarginisAvailableForPurchase: Boolean+ pricing: ProductPricingInfo}input ProductChannelListingAddInput {