Skip to content

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

Improve checkout performance; include product collections when calculating variant price

Piotr Blaszczyk8f99df03562b
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 70fa487307..49774989f2 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -692,7 +692,7 @@ def cancel_active_payments(checkout: Checkout):
def fetch_checkout_lines(checkout: Checkout) -> Iterable[CheckoutLineInfo]:
"""Fetch checkout lines as CheckoutLineInfo objects."""
- lines = CheckoutLine.objects.filter(checkout=checkout).prefetch_related(
+ lines = checkout.lines.prefetch_related(
"variant__product__collections",
"variant__channel_listings__channel",
"variant__product__product_type",
diff --git a/saleor/webhook/serializers.py b/saleor/webhook/serializers.py
index 228d1d0e10..7f0b5368eb 100644
--- a/saleor/webhook/serializers.py
+++ b/saleor/webhook/serializers.py
@@ -1,5 +1,7 @@
from typing import TYPE_CHECKING, List
+from ..checkout.utils import fetch_checkout_lines
+
if TYPE_CHECKING:
# pylint: disable=unused-import
from ..checkout.models import Checkout
@@ -8,16 +10,16 @@ if TYPE_CHECKING:
def serialize_checkout_lines(checkout: "Checkout") -> List[dict]:
data = []
channel = checkout.channel
- for line in checkout.lines.select_related("variant__product").all():
- variant = line.variant
- channel_listing = variant.channel_listings.get(channel=channel)
+ for line_info in fetch_checkout_lines(checkout):
+ variant = line_info.variant
+ channel_listing = line_info.channel_listing
+ collections = line_info.collections
product = variant.product
- # TODO: optimize getting arguments for get_price
- base_price = variant.get_price(product, [], channel, channel_listing)
+ base_price = variant.get_price(product, collections, channel, channel_listing)
data.append(
{
"sku": variant.sku,
- "quantity": line.quantity,
+ "quantity": line_info.line.quantity,
"base_price": str(base_price.amount),
"currency": channel.currency_code,
"full_name": variant.display_product(),