Skip to content

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

Add serialized CheckoutLines in the checkout webhook

Mateusz Dereniowskibcee97d8e0a2
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 592771493a..7af01236ac 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -4575,6 +4575,7 @@ enum WebhookEventTypeEnum {
ORDER_FULFILLED
CUSTOMER_CREATED
PRODUCT_CREATED
+ CHECKOUT_QUANTITY_CHANGED
}
input WebhookFilterInput {
diff --git a/saleor/webhook/payload_serializers.py b/saleor/webhook/payload_serializers.py
index c62c8be8f0..9c3fa9833e 100644
--- a/saleor/webhook/payload_serializers.py
+++ b/saleor/webhook/payload_serializers.py
@@ -21,6 +21,7 @@ class PayloadSerializer(JSONSerializer):
def serialize(self, queryset, **options):
self.additional_fields = options.pop("additional_fields", {})
+ self.extra_dict_data = options.pop("extra_dict_data", {})
self.obj_id_name = options.pop("obj_id_name", "id")
return super().serialize(
queryset,
@@ -40,6 +41,7 @@ class PayloadSerializer(JSONSerializer):
data = OrderedDict(
[("type", str(obj._meta.object_name)), (self.obj_id_name, obj_id)]
)
+ # Evaluate and add the "additional fields"
python_serializer = PythonSerializer()
for field_name, (qs, fields) in self.additional_fields.items():
data_to_serialize = qs(obj)
@@ -53,5 +55,8 @@ class PayloadSerializer(JSONSerializer):
data[field_name] = python_serializer.serialize(
[data_to_serialize], fields=fields
)[0]
+ # Update the data with the "extra dict data"
+ data.update(self.extra_dict_data)
+ # Finally update the data with the super class' "self._current" content
data.update(self._current)
return data
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 9b32a49621..2f39d3dff3 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -10,10 +10,10 @@ from ..payment import ChargeStatus
from ..product.models import Product
from .event_types import WebhookEventType
from .payload_serializers import PayloadSerializer
+from .serializers import serialize_checkout_lines
if TYPE_CHECKING:
- # flake8: noqa
- from ..checkout.models import Checkout, CheckoutLine
+ from ..checkout.models import Checkout
ADDRESS_FIELDS = (
@@ -116,7 +116,8 @@ def generate_checkout_payload(checkout: "Checkout"):
)
user_fields = ("email", "first_name", "last_name")
shipping_method_fields = ("name", "type", "currency", "price_amount")
- lines_fields = ("quantity",)
+ lines_dict_data = serialize_checkout_lines(checkout)
+
checkout_data = serializer.serialize(
[checkout],
fields=checkout_fields,
@@ -126,7 +127,10 @@ def generate_checkout_payload(checkout: "Checkout"):
"billing_address": (lambda c: c.billing_address, ADDRESS_FIELDS),
"shipping_address": (lambda c: c.shipping_address, ADDRESS_FIELDS),
"shipping_method": (lambda c: c.shipping_method, shipping_method_fields),
- "lines": (lambda c: c.lines.all(), lines_fields),
+ },
+ extra_dict_data={
+ # Casting to list to make it json-serializable
+ "lines": list(lines_dict_data)
},
)
return checkout_data
diff --git a/saleor/webhook/serializers.py b/saleor/webhook/serializers.py
new file mode 100644
index 0000000000..0aad1ca3b1
--- /dev/null
+++ b/saleor/webhook/serializers.py
@@ -0,0 +1,23 @@
+from typing import TYPE_CHECKING, List
+
+if TYPE_CHECKING:
+ from ..checkout.models import Checkout
+
+
+def serialize_checkout_lines(checkout: "Checkout") -> List[dict]:
+ data = []
+ for line in checkout.lines.prefetch_related("variant__product").all():
+ variant = line.variant
+ product = variant.product
+ data.append(
+ {
+ "sku": variant.sku,
+ "quantity": line.quantity,
+ "base_price": str(variant.base_price.amount),
+ "currency": variant.currency,
+ "full_name": variant.display_product(),
+ "product_name": product.name,
+ "variant_name": variant.name,
+ }
+ )
+ return data