Skip to content

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

Change places of calls for checkout_qquantity_changes. Fix bug with user passing as a SimpleLazyObejct

Maciej Korycinski53a0ce5d8903
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index b012815040..a380d583c9 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -99,9 +99,6 @@ def update_checkout_quantity(checkout):
checkout.quantity = total_lines
checkout.save(update_fields=["quantity"])
- manager = get_plugins_manager()
- manager.checkout_quantity_changed(checkout)
-
def check_variant_in_stock(
checkout, variant, quantity=1, replace=False, check_quantity=True
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index cbcf16975a..df2ace38d8 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -287,6 +287,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):
"Can't create checkout with unpublished product.",
code=exc.code,
)
+ info.context.plugins.checkout_quantity_changed(instance)
# Save provided addresses and associate them to the checkout
cls.save_addresses(instance, cleaned_input)
@@ -361,6 +362,7 @@ class CheckoutLinesAdd(BaseMutation):
raise ValidationError(
"Can't add unpublished product.", code=exc.code,
)
+ info.context.plugins.checkout_quantity_changed(checkout)
lines = list(checkout)
@@ -408,6 +410,7 @@ class CheckoutLineDelete(BaseMutation):
if line and line in checkout.lines.all():
line.delete()
+ info.context.plugins.checkout_quantity_changed(checkout)
lines = list(checkout)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index d330329c19..d2c52ba368 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -115,8 +115,35 @@ MUTATION_CHECKOUT_CREATE = """
"""
-def test_checkout_create(api_client, stock, graphql_address_data):
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")
+def test_checkout_create_triggers_webhooks(
+ mocked_webhook_trigger, user_api_client, stock, graphql_address_data, settings
+):
+ """Create checkout object using GraphQL API."""
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ variant = stock.product_variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ test_email = "[email protected]"
+ shipping_address = graphql_address_data
+ variables = {
+ "checkoutInput": {
+ "lines": [{"quantity": 1, "variantId": variant_id}],
+ "email": test_email,
+ "shippingAddress": shipping_address,
+ }
+ }
+ assert not Checkout.objects.exists()
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+ get_graphql_content(response)["data"]["checkoutCreate"]
+
+ assert mocked_webhook_trigger.called
+
+
+def test_checkout_create(
+ _mocked_webhook_trigger, api_client, stock, graphql_address_data, settings
+):
"""Create checkout object using GraphQL API."""
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
test_email = "[email protected]"
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 29aac5256c..5bfdb82114 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -30,37 +30,42 @@ class WebhookPlugin(BasePlugin):
def order_created(self, order: "Order", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ order.refresh_from_db()
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_CREATED, order_data)
def order_fully_paid(self, order: "Order", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ order.refresh_from_db()
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_FULLY_PAID, order_data)
def order_updated(self, order: "Order", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ order.refresh_from_db()
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_UPDATED, order_data)
def order_cancelled(self, order: "Order", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ order.refresh_from_db()
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_CANCELLED, order_data)
def order_fulfilled(self, order: "Order", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ order.refresh_from_db()
order_data = generate_order_payload(order)
trigger_webhooks_for_event.delay(WebhookEventType.ORDER_FULFILLED, order_data)
def fulfillment_created(self, fulfillment: "Fulfillment", previous_value):
if not self.active:
return previous_value
-
+ fulfillment.refresh_from_db()
fulfillment_data = generate_fulfillment_payload(fulfillment)
trigger_webhooks_for_event.delay(
WebhookEventType.FULFILLMENT_CREATED, fulfillment_data
@@ -69,7 +74,7 @@ class WebhookPlugin(BasePlugin):
def customer_created(self, customer: "User", previous_value: Any) -> Any:
if not self.active:
return previous_value
-
+ customer.refresh_from_db()
customer_data = generate_customer_payload(customer)
trigger_webhooks_for_event.delay(
WebhookEventType.CUSTOMER_CREATED, customer_data
@@ -78,6 +83,7 @@ class WebhookPlugin(BasePlugin):
def product_created(self, product: "Product", previous_value: Any) -> Any:
if not self.active:
return previous_value
+ product.refresh_from_db()
product_data = generate_product_payload(product)
trigger_webhooks_for_event.delay(WebhookEventType.PRODUCT_CREATED, product_data)
@@ -86,6 +92,7 @@ class WebhookPlugin(BasePlugin):
) -> Any:
if not self.active:
return previous_value
+ checkout.refresh_from_db()
checkout_data = generate_checkout_payload(checkout)
trigger_webhooks_for_event.delay(
WebhookEventType.CHECKOUT_QUANTITY_CHANGED, checkout_data