Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discounts resolver to work like in docs #15883

Merged
merged 4 commits into from May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions saleor/graphql/core/types/taxes.py
Expand Up @@ -375,16 +375,16 @@ def resolve_discounts(root: Union[Checkout, Order], info: ResolveInfo):
if isinstance(root, Checkout):

def calculate_checkout_discounts(checkout_info):
is_shipping_voucher = (
checkout_info.voucher.type == VoucherType.SHIPPING
if checkout_info.voucher
else False
)
checkout = checkout_info.checkout
discount_name = checkout.discount_name
return (
[{"name": discount_name, "amount": checkout.discount}]
if checkout.discount and not is_shipping_voucher
if checkout.discount
and (
checkout_info.voucher
and checkout_info.voucher.type == VoucherType.ENTIRE_ORDER
and not checkout_info.voucher.apply_once_per_order
)
else []
)

Expand Down
Expand Up @@ -192,7 +192,7 @@ def test_checkout_calculate_taxes_with_free_shipping_voucher(


@freeze_time("2020-03-18 12:00:00")
def test_checkout_calculate_taxes_with_voucher(
def test_checkout_calculate_taxes_with_entire_order_voucher(
checkout_with_voucher,
webhook_app,
permission_handle_taxes,
Expand Down Expand Up @@ -246,6 +246,64 @@ def test_checkout_calculate_taxes_with_voucher(
}


@freeze_time("2020-03-18 12:00:00")
def test_checkout_calculate_taxes_with_entire_order_voucher_once_per_order(
voucher,
checkout_with_voucher,
webhook_app,
permission_handle_taxes,
):
# given
webhook_app.permissions.add(permission_handle_taxes)
webhook = Webhook.objects.create(
name="Webhook",
app=webhook_app,
target_url="http://www.example.com/any",
subscription_query=TAXES_SUBSCRIPTION_QUERY,
)
event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXES
webhook.events.create(event_type=event_type)
voucher.apply_once_per_order = True
voucher.save()

# when
deliveries = create_delivery_for_subscription_sync_event(
event_type, checkout_with_voucher, webhook
)

# then
assert json.loads(deliveries.payload.payload) == {
"__typename": "CalculateTaxes",
"taxBase": {
"address": None,
"currency": "USD",
"discounts": [],
"channel": {"id": to_global_id_or_none(checkout_with_voucher.channel)},
"lines": [
{
"chargeTaxes": True,
"productName": "Test product",
"productSku": "123",
"quantity": 3,
"sourceLine": {
"id": to_global_id_or_none(checkout_with_voucher.lines.first()),
"__typename": "CheckoutLine",
},
"totalPrice": {"amount": 20.0},
"unitPrice": {"amount": 6.67},
"variantName": "",
}
],
"pricesEnteredWithTax": True,
"shippingPrice": {"amount": 0.0},
"sourceObject": {
"id": to_global_id_or_none(checkout_with_voucher),
"__typename": "Checkout",
},
},
}


@freeze_time("2020-03-18 12:00:00")
def test_checkout_calculate_taxes_with_shipping_voucher(
checkout_with_voucher,
Expand Down