Skip to content

Commit

Permalink
Merge pull request #15885 from saleor/fix-discounts-resolver-319
Browse files Browse the repository at this point in the history
Fix discounts resolver to work like in docs
  • Loading branch information
aniav committed Apr 30, 2024
2 parents 3296147 + 06a26a4 commit 1c4ac93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
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

0 comments on commit 1c4ac93

Please sign in to comment.