Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move the stock information of a product/variant to the type itself
NyanKiyoshica394bb32f5c
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 3bc38dc0a9..3e0e3cd86d 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -184,7 +184,9 @@ class ProductOrder(graphene.InputObjectType):class BasePricingInfo(graphene.ObjectType):available = graphene.Boolean(- description='Whether it is in stock and visible or not.')+ description='Whether it is in stock and visible or not.',+ deprecation_reason=(+ 'This has been moved to the parent type as \'is_available\'.'))on_sale = graphene.Boolean(description='Whether it is in sale or not.')discount = graphene.Field(@@ -253,6 +255,8 @@ class ProductVariant(CountableDjangoObjectType):description=dedent("""Lists the storefront product's pricing,the current price and discounts, only meant for displaying"""))+ is_available = graphene.Boolean(+ description='Whether the variant is in stock and visible or not.')attributes = graphene.List(graphene.NonNull(SelectedAttribute), required=True,description='List of attributes assigned to this variant.')@@ -326,6 +330,9 @@ class ProductVariant(CountableDjangoObjectType):resolve_availability = resolve_pricing+ def resolve_is_available(self, _info):+ return self.is_available+@permission_required('product.manage_products')def resolve_price_override(self, *_args):return self.price_override@@ -385,6 +392,8 @@ class Product(CountableDjangoObjectType):ProductPricingInfo, description=dedent("""Lists the storefront product's pricing,the current price and discounts, only meant for displaying."""))+ is_available = graphene.Boolean(+ description='Whether the product is in stock and visible or not.')base_price = graphene.Field(Money,description='The product\'s default base price.')@@ -473,6 +482,9 @@ class Product(CountableDjangoObjectType):resolve_availability = resolve_pricing+ def resolve_is_available(self, _info):+ return self.is_available+@permission_required('product.manage_products')def resolve_price(self, _info):return self.pricediff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 1ae74c1ce9..6168e0c9bd 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -1686,7 +1686,9 @@ type Product implements Node {thumbnail(size: Int): Imageavailability: ProductPricingInfo @deprecated(reason: "Has been renamed to 'pricing'.")pricing: ProductPricingInfo- price: Money @deprecated(reason: "Has been replaced by 'pricing.price_range_undiscounted'")+ isAvailable: Boolean+ basePrice: Money+ price: Money @deprecated(reason: "Has been replaced by 'basePrice'")taxRate: TaxRateTypeattributes: [SelectedAttribute!]!purchaseCost: MoneyRange@@ -1827,7 +1829,7 @@ enum ProductOrderField {}type ProductPricingInfo {- available: Boolean+ available: Boolean @deprecated(reason: "This has been moved to the parent type as 'is_available'.")onSale: Booleandiscount: TaxedMoneydiscountLocalCurrency: TaxedMoney@@ -1922,9 +1924,10 @@ type ProductVariant implements Node {weight: WeightstockQuantity: Int!priceOverride: Money- price: Money+ price: Money @deprecated(reason: "Has been replaced by 'pricing.price_undiscounted'")availability: VariantPricingInfo @deprecated(reason: "Has been renamed to 'pricing'.")pricing: VariantPricingInfo+ isAvailable: Booleanattributes: [SelectedAttribute!]!costPrice: Moneymargin: Int@@ -2581,7 +2584,7 @@ type VariantImageUnassign {}type VariantPricingInfo {- available: Boolean+ available: Boolean @deprecated(reason: "This has been moved to the parent type as 'is_available'.")onSale: Booleandiscount: TaxedMoneydiscountLocalCurrency: TaxedMoneydiff --git a/saleor/product/models.py b/saleor/product/models.pyindex dd7b6836b8..493e8cd21b 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -143,6 +143,10 @@ class Product(SeoModel, PublishableModel):def __str__(self):return self.name+ @property+ def is_available(self):+ return self.is_visible and self.is_in_stock()+def get_absolute_url(self):return reverse('product:details',@@ -231,6 +235,10 @@ class ProductVariant(models.Model):def is_visible(self):return self.product.is_visible+ @property+ def is_available(self):+ return self.product.is_in_stock()+def check_quantity(self, quantity):"""Check if there is at least the given quantity in stockif stock handling is enabled.diff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.pyindex 4945c7feab..eca7f19896 100644--- a/saleor/product/utils/availability.py+++ b/saleor/product/utils/availability.py@@ -101,12 +101,10 @@ def get_product_availability(price_range_local, discount_local_currency = _get_product_price_range(discounted, undiscounted, local_currency)- is_visible = product.is_visible- is_available = product.is_in_stock() and is_visible- is_on_sale = is_visible and discount is not None+ is_on_sale = product.is_visible and discount is not Nonereturn ProductAvailability(- available=is_available,+ available=product.is_available,on_sale=is_on_sale,price_range=discounted,price_range_undiscounted=undiscounted,@@ -132,12 +130,10 @@ def get_variant_availability(price_local_currency = Nonediscount_local_currency = None- is_visible = variant.is_visible- is_available = variant.is_in_stock() and is_visible- is_on_sale = is_visible and discount is not None+ is_on_sale = variant.is_visible and discount is not Nonereturn VariantAvailability(- available=is_available,+ available=variant.is_available,on_sale=is_on_sale,price=discounted,price_undiscounted=undiscounted,diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 768923580d..5ac87af192 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -103,6 +103,7 @@ def test_product_query(staff_api_client, product, permission_manage_products):namestockQuantity}+ isAvailablepricing {available,priceRange {@@ -156,6 +157,8 @@ def test_product_query(staff_api_client, product, permission_manage_products):'purchaseCost']['start']['amount']assert purchase_cost.stop.amount == product_data['purchaseCost']['stop']['amount']+ assert product_data['isAvailable'] is product.is_visible+ assert product_data['pricing']['available'] is product.is_visibleassert margin[0] == product_data['margin']['start']assert margin[1] == product_data['margin']['stop']