Skip to content

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

Extend existing tests for sales (#17122)

Anna Szczęch1aa0084ba6da
diff --git a/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_fixed_sale.py b/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_fixed_sale.py
index 9cc6a8e8d5..e9f13ae652 100644
--- a/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_fixed_sale.py
+++ b/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_fixed_sale.py
@@ -1,7 +1,7 @@
import pytest
from ......product.tasks import recalculate_discounted_price_for_products_task
-from ....product.utils.preparing_product import prepare_product
+from ....product.utils.preparing_product import prepare_products
from ....sales.utils import (
create_sale,
create_sale_channel_listing,
@@ -14,13 +14,16 @@ from ...utils import (
checkout_create,
checkout_delivery_method_update,
checkout_dummy_payment_create,
+ checkout_lines_add,
+ checkout_lines_delete,
+ checkout_lines_update,
)
-def prepare_sale_for_product(
+def prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_id,
+ product_ids,
sale_discount_type,
sale_discount_value,
):
@@ -42,14 +45,14 @@ def prepare_sale_for_product(
sale_id,
add_channels=sale_listing_input,
)
- catalogue_input = {"products": [product_id]}
+ catalogue_input = {"products": product_ids}
sale_catalogues_add(
e2e_staff_api_client,
sale_id,
catalogue_input,
)
- return sale_id, sale_discount_value
+ return sale_id
@pytest.mark.e2e
@@ -74,31 +77,57 @@ def test_checkout_products_on_fixed_sale_core_1002(
warehouse_id = shop_data["warehouse"]["id"]
shipping_method_id = shop_data["shipping_method"]["id"]
- (
- product_id,
- product_variant_id,
- product_variant_price,
- ) = prepare_product(
+ products_data = prepare_products(
e2e_staff_api_client,
warehouse_id,
channel_id,
- variant_price="13.33",
+ ["9.99", "12", "102.33", "20.5"],
)
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
- sale_id, sale_discount_value = prepare_sale_for_product(
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
+
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+ fixed_sale_discount_value = 5
+ sale_id = prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_id,
+ product_ids,
sale_discount_type="FIXED",
- sale_discount_value=3,
+ sale_discount_value=fixed_sale_discount_value,
)
+
# prices are updated in the background, we need to force it to retrieve the correct
# ones
recalculate_discounted_price_for_products_task()
+ product1_quantity = 3
+ product2_quantity = 1
+ product3_quantity = 2
+ product4_quantity = 2
+
# Step 1 - checkoutCreate for product on sale
+
lines = [
- {"variantId": product_variant_id, "quantity": 2},
+ {
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
+ },
]
checkout_data = checkout_create(
e2e_not_logged_api_client,
@@ -109,46 +138,266 @@ def test_checkout_products_on_fixed_sale_core_1002(
set_default_shipping_address=True,
)
checkout_id = checkout_data["id"]
- checkout_lines = checkout_data["lines"][0]
- unit_price = float(product_variant_price) - sale_discount_value
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_unit_price = product1_variant_price - fixed_sale_discount_value
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
+ )
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
+
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_unit_price = product2_variant_price - fixed_sale_discount_value
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
+ )
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_not_logged_api_client,
+ checkout_id,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_unit_price = product3_variant_price - fixed_sale_discount_value
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == product4_quantity
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
- assert checkout_data["isShippingRequired"] is True
- assert checkout_lines["unitPrice"]["gross"]["amount"] == unit_price
- assert checkout_lines["undiscountedUnitPrice"]["amount"] == float(
- product_variant_price
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_unit_price = product2_variant_price - fixed_sale_discount_value
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
)
- # Step 2 - Set DeliveryMethod for checkout.
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_unit_price = product3_variant_price - fixed_sale_discount_value
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_subtotal = round(
+ product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ product2_new_quantity = 3
+ product3_new_quantity = 1
+
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_unit_price = product2_variant_price - fixed_sale_discount_value
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_new_quantity, 2
+ )
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_unit_price = product3_variant_price - fixed_sale_discount_value
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_new_quantity, 2
+ )
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_subtotal = round(
+ product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Set DeliveryMethod for checkout.
checkout_data = checkout_delivery_method_update(
e2e_not_logged_api_client,
checkout_id,
shipping_method_id,
)
assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
- # Step 3 - Create payment for checkout.
+ # Step 6 - Create payment for checkout.
checkout_dummy_payment_create(
e2e_not_logged_api_client,
checkout_id,
total_gross_amount,
)
- # Step 5 - Complete checkout.
+ # Step 7 - Complete checkout.
order_data = checkout_complete(
e2e_not_logged_api_client,
checkout_id,
)
assert order_data["status"] == "UNFULFILLED"
- assert order_data["total"]["gross"]["amount"] == total_gross_amount
- assert order_data["subtotal"]["gross"]["amount"] == subtotal_gross_amount
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
+
+ assert len(order_data["lines"]) == 3
+
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert order_line1["unitDiscount"]["amount"] == fixed_sale_discount_value
+ assert order_line1["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_new_quantity, 2
+ )
- order_line = order_data["lines"][0]
- assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
- product_variant_price
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert order_line2["unitDiscount"]["amount"] == fixed_sale_discount_value
+ assert order_line2["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
)
- assert order_line["unitDiscountType"] == "FIXED"
- assert order_line["unitPrice"]["gross"]["amount"] == unit_price
- assert order_line["unitDiscount"]["amount"] == float(sale_discount_value)
- assert order_line["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert order_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_new_quantity, 2
+ )
+
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert order_line3["unitDiscount"]["amount"] == 0
+ assert order_line3["unitDiscountReason"] is None
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
+ )
+ assert order_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
diff --git a/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_percentage_sale.py b/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_percentage_sale.py
index d15e3a228c..82d1174bcd 100644
--- a/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_percentage_sale.py
+++ b/saleor/tests/e2e/checkout/discounts/sales/test_checkout_product_on_percentage_sale.py
@@ -1,7 +1,7 @@
import pytest
from ......product.tasks import recalculate_discounted_price_for_products_task
-from ....product.utils.preparing_product import prepare_product
+from ....product.utils.preparing_product import prepare_products
from ....sales.utils import (
create_sale,
create_sale_channel_listing,
@@ -14,13 +14,16 @@ from ...utils import (
checkout_create,
checkout_delivery_method_update,
checkout_dummy_payment_create,
+ checkout_lines_add,
+ checkout_lines_delete,
+ checkout_lines_update,
)
-def prepare_sale_for_product(
+def prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_id,
+ product_ids,
sale_discount_type,
sale_discount_value,
):
@@ -42,7 +45,7 @@ def prepare_sale_for_product(
sale_id,
add_channels=sale_listing_input,
)
- catalogue_input = {"products": [product_id]}
+ catalogue_input = {"products": product_ids}
sale_catalogues_add(
e2e_staff_api_client,
sale_id,
@@ -74,21 +77,33 @@ def test_checkout_products_on_percentage_sale_core_1004(
warehouse_id = shop_data["warehouse"]["id"]
shipping_method_id = shop_data["shipping_method"]["id"]
- (
- product_id,
- product_variant_id,
- product_variant_price,
- ) = prepare_product(
+ products_data = prepare_products(
e2e_staff_api_client,
warehouse_id,
channel_id,
- variant_price="13.33",
+ ["13.33", "15", "9.99", "20.5"],
)
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
- sale_id, sale_discount_value = prepare_sale_for_product(
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
+
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+
+ sale_id, sale_discount_value = prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_id,
+ product_ids,
sale_discount_type="PERCENTAGE",
sale_discount_value=3,
)
@@ -97,11 +112,20 @@ def test_checkout_products_on_percentage_sale_core_1004(
# ones
recalculate_discounted_price_for_products_task()
+ product1_quantity = 2
+ product2_quantity = 4
+ product3_quantity = 1
+ product4_quantity = 1
+
# Step 1 - checkoutCreate for product on sale
lines = [
{
- "variantId": product_variant_id,
- "quantity": 2,
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
},
]
checkout_data = checkout_create(
@@ -113,47 +137,272 @@ def test_checkout_products_on_percentage_sale_core_1004(
set_default_shipping_address=True,
)
checkout_id = checkout_data["id"]
- checkout_lines = checkout_data["lines"][0]
- line_discount = round(float(product_variant_price) * sale_discount_value / 100, 2)
- unit_price = float(product_variant_price) - line_discount
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
- assert checkout_data["isShippingRequired"] is True
- assert checkout_lines["unitPrice"]["gross"]["amount"] == unit_price
- assert checkout_lines["undiscountedUnitPrice"]["amount"] == float(
- product_variant_price
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_discount = round(product1_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product1_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
)
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
- # Step 2 - Set DeliveryMethod for checkout.
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = product2_variant_price - line2_discount
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
+ )
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_not_logged_api_client,
+ checkout_id,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line3_unit_price = product3_variant_price - line3_discount
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == 1
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ )
+
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = product3_variant_price - line2_discount
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == 1
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_subtotal = round(
+ +product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ product2_new_quantity = 3
+ product3_new_quantity = 2
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_new_quantity, 2
+ )
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = product3_variant_price - line2_discount
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_new_quantity, 2
+ )
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_subtotal = round(
+ +product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Set DeliveryMethod for checkout.
checkout_data = checkout_delivery_method_update(
e2e_not_logged_api_client,
checkout_id,
shipping_method_id,
)
assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
- # Step 3 - Create payment for checkout.
+ # Step 6 - Create payment for checkout.
checkout_dummy_payment_create(
e2e_not_logged_api_client,
checkout_id,
total_gross_amount,
)
- # Step 5 - Complete checkout.
+ # Step 7 - Complete checkout.
order_data = checkout_complete(
e2e_not_logged_api_client,
checkout_id,
)
assert order_data["status"] == "UNFULFILLED"
- assert order_data["total"]["gross"]["amount"] == total_gross_amount
- assert order_data["subtotal"]["gross"]["amount"] == subtotal_gross_amount
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
+
+ assert len(order_data["lines"]) == 3
+
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert order_line1["unitDiscount"]["amount"] == line1_discount
+ assert order_line1["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_new_quantity, 2
+ )
+
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert order_line2["unitDiscount"]["amount"] == line2_discount
+ assert order_line2["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
+ )
+ assert order_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_new_quantity, 2
+ )
- order_line = order_data["lines"][0]
- assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
- product_variant_price
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert order_line3["unitDiscount"]["amount"] == 0
+ assert order_line3["unitDiscountReason"] is None
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
)
- assert order_line["unitDiscountType"] == "FIXED"
- assert order_line["unitPrice"]["gross"]["amount"] == unit_price
- assert order_line["unitDiscount"]["amount"] == line_discount
- assert order_line["unitDiscountReason"] == f"Sale: {sale_id}"
+ assert order_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
diff --git a/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py b/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
index a291836b36..1bfcf4f735 100644
--- a/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
+++ b/saleor/tests/e2e/checkout/discounts/test_checkout_calculate_correct_discount_for_sale_and_voucher.py
@@ -1,9 +1,13 @@
import pytest
from .....product.tasks import recalculate_discounted_price_for_products_task
-from ...product.utils.preparing_product import prepare_product
-from ...sales.utils import create_sale, create_sale_channel_listing, sale_catalogues_add
-from ...shop.utils.preparing_shop import prepare_default_shop
+from ...product.utils.preparing_product import prepare_products
+from ...sales.utils import (
+ create_sale,
+ create_sale_channel_listing,
+ sale_catalogues_add,
+)
+from ...shop.utils import prepare_default_shop
from ...utils import assign_permissions
from ...vouchers.utils import create_voucher, create_voucher_channel_listing
from ..utils import (
@@ -13,17 +17,20 @@ from ..utils import (
checkout_delivery_method_update,
checkout_dummy_payment_create,
checkout_lines_add,
+ checkout_lines_delete,
+ checkout_lines_update,
)
-def prepare_sale_for_variant(
+def prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_variant_id,
+ product_ids,
sale_discount_type,
sale_discount_value,
+ sale_name,
):
- sale_name = "Sale"
+ sale_name = sale_name
sale = create_sale(
e2e_staff_api_client,
sale_name,
@@ -41,7 +48,7 @@ def prepare_sale_for_variant(
sale_id,
add_channels=sale_listing_input,
)
- catalogue_input = {"variants": [product_variant_id]}
+ catalogue_input = {"products": product_ids}
sale_catalogues_add(
e2e_staff_api_client,
sale_id,
@@ -81,38 +88,12 @@ def prepare_voucher(
@pytest.mark.e2e
- (
- "variant_price",
- "sale_discount_value",
- "sale_discount_type",
- "expected_sale_discount",
- "voucher_discount_value",
- "voucher_discount_type",
- "expected_voucher_discount",
- "expected_unit_price",
- ),
- [
- ("19.99", 13, "PERCENTAGE", 2.60, 13, "PERCENTAGE", 2.26, 15.13),
- ("30", 10, "FIXED", 10, 5, "FIXED", 5, 17.5),
- ("56.50", 19.99, "FIXED", 19.99, 33, "PERCENTAGE", 12.05, 24.46),
- ("77.77", 17.5, "PERCENTAGE", 13.61, 25, "FIXED", 25, 51.66),
- ],
-)
-def test_checkout_calculate_discount_for_sale_and_voucher_1014(
+def test_checkout_calculate_discount_for_percentage_sale_and_percentage_voucher_CORE_0114(
e2e_not_logged_api_client,
e2e_staff_api_client,
shop_permissions,
permission_manage_product_types_and_attributes,
permission_manage_discounts,
- variant_price,
- sale_discount_value,
- sale_discount_type,
- expected_sale_discount,
- voucher_discount_value,
- voucher_discount_type,
- expected_voucher_discount,
- expected_unit_price,
):
# Before
permissions = [
@@ -128,40 +109,485 @@ def test_checkout_calculate_discount_for_sale_and_voucher_1014(
warehouse_id = shop_data["warehouse"]["id"]
shipping_method_id = shop_data["shipping_method"]["id"]
- (
- _product_id,
- product_variant_id,
- product_variant_price,
- ) = prepare_product(
+ products_data = prepare_products(
e2e_staff_api_client,
warehouse_id,
channel_id,
- variant_price,
+ ["13.33", "16", "9.99", "20.5"],
)
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
+
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
- sale_id, sale_discount_value = prepare_sale_for_variant(
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+
+ sale_id, sale_discount_value = prepare_sale_for_products(
e2e_staff_api_client,
channel_id,
- product_variant_id,
- sale_discount_type,
- sale_discount_value,
+ product_ids,
+ sale_discount_type="PERCENTAGE",
+ sale_discount_value=13,
+ sale_name="Sale_Percentage",
)
+ # prices are updated in the background, we need to force it to retrieve the correct
+ # ones
+ recalculate_discounted_price_for_products_task()
+
voucher_discount_value, voucher_code = prepare_voucher(
e2e_staff_api_client,
channel_id,
"VOUCHER001",
- voucher_discount_type,
- voucher_discount_value,
+ voucher_discount_type="PERCENTAGE",
+ voucher_discount_value=13,
+ )
+ product1_quantity = 2
+ product2_quantity = 3
+ product3_quantity = 4
+ product4_quantity = 1
+ product2_new_quantity = 2
+ product3_new_quantity = 3
+
+ # Step 1 - checkoutCreate for product on sale
+ lines = [
+ {
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
+ },
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_discount = round(product1_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product1_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
+ )
+
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = product2_variant_price - line2_discount
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_not_logged_api_client,
+ checkout_id,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line3_unit_price = product3_variant_price - line3_discount
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == 1
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ )
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == 1
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ +product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_line1_total = round(product2_new_quantity * line1_unit_price, 2)
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ calculated_line2_total = round(product3_new_quantity * line2_unit_price, 2)
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_line3_total = round(product4_quantity * line3_unit_price, 2)
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ calculated_subtotal = round(
+ +product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ calculated_discount = round(calculated_subtotal * voucher_discount_value / 100, 2)
+ assert checkout_data["discount"]["amount"] == calculated_discount
+ assert checkout_data["voucherCode"] == voucher_code
+
+ # Assert lines after voucher code
+ assert len(checkout_data["lines"]) == 3
+ checkout_line1 = checkout_data["lines"][0]
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line1["quantity"] == product2_new_quantity
+
+ calculated_line1_discount = round(
+ calculated_line1_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line1_total = round(
+ calculated_line1_total - calculated_line1_discount, 2
+ )
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ checkout_line2 = checkout_data["lines"][1]
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line2["quantity"] == product3_new_quantity
+
+ calculated_line2_discount = round(
+ calculated_line2_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line2_total = round(
+ calculated_line2_total - calculated_line2_discount, 2
+ )
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product4_variant_price
+ assert checkout_line3["quantity"] == product4_quantity
+
+ calculated_line3_discount = round(
+ calculated_discount - calculated_line1_discount - calculated_line2_discount, 2
+ )
+ calculated_line3_total = round(
+ calculated_line3_total - calculated_line3_discount, 2
+ )
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+
+ # Assert total and subtotal after voucher code
+ calculated_subtotal = round(calculated_subtotal - calculated_discount, 2)
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+ calculated_total = calculated_subtotal
+ assert checkout_data["totalPrice"]["gross"]["amount"] == calculated_total
+
+ # Step 6 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
+
+ # Step 7 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ )
+
+ # Step 8 - Complete checkout.
+ order_data = checkout_complete(
+ e2e_not_logged_api_client,
+ checkout_id,
+ )
+ assert len(order_data["lines"]) == 3
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
+
+ assert order_data["discounts"][0]["type"] == "VOUCHER"
+ assert order_data["voucher"]["code"] == voucher_code
+ assert order_data["discounts"][0]["value"] == calculated_discount
+
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+ assert order_line1["unitPrice"]["gross"]["amount"] == round(
+ calculated_line1_total / product2_new_quantity, 2
+ )
+ assert order_line1["unitDiscount"]["amount"] == round(
+ line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ )
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
+ )
+ assert order_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+ assert order_line2["unitPrice"]["gross"]["amount"] == round(
+ calculated_line2_total / product3_new_quantity, 2
+ )
+ assert order_line2["unitDiscount"]["amount"] == round(
+ line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ )
+
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code}"
+ )
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
+ )
+ assert order_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ assert order_line3["unitPrice"]["gross"]["amount"] == round(
+ calculated_line3_total / product4_quantity, 2
+ )
+ assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+
+
+def test_checkout_calculate_discount_for_fixed_sale_and_fixed_voucher_CORE_0114(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+):
+ # Before
+ permissions = [
+ *shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+
+ products_data = prepare_products(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ ["13.33", "16", "19.99", "20.5"],
+ )
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
+
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
+
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+
+ sale_id, sale_discount_value = prepare_sale_for_products(
+ e2e_staff_api_client,
+ channel_id,
+ product_ids,
+ sale_discount_type="FIXED",
+ sale_discount_value=10,
+ sale_name="Sale_FIXED",
)
# prices are updated in the background, we need to force it to retrieve the correct
# ones
recalculate_discounted_price_for_products_task()
- # Step 1 - Create checkout for product on sale
+ voucher_discount_value, voucher_code = prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ "VOUCHER002",
+ voucher_discount_type="FIXED",
+ voucher_discount_value=5,
+ )
+ product1_quantity = 2
+ product2_quantity = 3
+ product3_quantity = 4
+ product4_quantity = 1
+ product2_new_quantity = 2
+ product3_new_quantity = 3
+
+ # Step 1 - checkoutCreate for product on sale
lines = [
- {"variantId": product_variant_id, "quantity": 1},
+ {
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
+ },
]
checkout_data = checkout_create(
e2e_not_logged_api_client,
@@ -172,38 +598,243 @@ def test_checkout_calculate_discount_for_sale_and_voucher_1014(
set_default_shipping_address=True,
)
checkout_id = checkout_data["id"]
- checkout_lines = checkout_data["lines"][0]
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
- assert checkout_data["isShippingRequired"] is True
- unit_price_on_sale = round(float(product_variant_price - expected_sale_discount), 2)
- assert checkout_lines["unitPrice"]["gross"]["amount"] == unit_price_on_sale
- assert checkout_lines["undiscountedUnitPrice"]["amount"] == product_variant_price
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product1_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
+ )
- # Step 2 Add voucher code to checkout
- checkout_data = checkout_add_promo_code(
- e2e_not_logged_api_client, checkout_id, voucher_code
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = round(product2_variant_price - line2_discount, 2)
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
)
- assert checkout_data["discount"]["amount"] == expected_voucher_discount
- assert checkout_data["lines"][0]["unitPrice"]["gross"]["amount"] == round(
- float(unit_price_on_sale - expected_voucher_discount), 2
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
)
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
- # Step 3 Add variant to the checkout
- lines_add = [
- {"variantId": product_variant_id, "quantity": 1},
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
]
checkout_data = checkout_lines_add(
- e2e_staff_api_client,
+ e2e_not_logged_api_client,
checkout_id,
- lines_add,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_discount = sale_discount_value
+ line3_unit_price = round(product3_variant_price - line3_discount, 2)
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == 1
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product2_variant_price - line1_discount
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ )
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == 1
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ +product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product2_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_line1_total = round(product2_new_quantity * line1_unit_price, 2)
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ calculated_line2_total = round(product3_new_quantity * line2_unit_price, 2)
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_line3_total = round(product4_quantity * line3_unit_price, 2)
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ calculated_subtotal = round(
+ +product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ calculated_discount = voucher_discount_value
+ assert checkout_data["discount"]["amount"] == calculated_discount
+ assert checkout_data["voucherCode"] == voucher_code
+
+ # Assert lines after voucher code
+ assert len(checkout_data["lines"]) == 3
+ checkout_line1 = checkout_data["lines"][0]
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line1["quantity"] == product2_new_quantity
+
+ # jak dzielic discount na linie przy fixed voucher
+ calculated_line1_discount = round(
+ calculated_line1_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line1_total = round(
+ calculated_line1_total - calculated_line1_discount, 2
+ )
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ checkout_line2 = checkout_data["lines"][1]
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line2["quantity"] == product3_new_quantity
+
+ calculated_line2_discount = round(
+ calculated_line2_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line2_total = round(
+ calculated_line2_total - calculated_line2_discount, 2
+ )
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product4_variant_price
+ assert checkout_line3["quantity"] == product4_quantity
+
+ calculated_line3_discount = round(
+ calculated_discount - calculated_line1_discount - calculated_line2_discount, 2
+ )
+ calculated_line3_total = round(
+ calculated_line3_total - calculated_line3_discount, 2
)
- checkout_lines = checkout_data["lines"][0]
- assert checkout_lines["quantity"] == 2
- assert checkout_lines["unitPrice"]["gross"]["amount"] == expected_unit_price
- subtotal_amount = expected_unit_price * 2
- assert checkout_lines["totalPrice"]["gross"]["amount"] == subtotal_amount
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
- # Step 4 - Set DeliveryMethod for checkout.
+ # Assert total and subtotal after voucher code
+ calculated_subtotal = round(calculated_subtotal - calculated_discount, 2)
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+ calculated_total = calculated_subtotal
+ assert checkout_data["totalPrice"]["gross"]["amount"] == calculated_total
+
+ # Step 6 - Set DeliveryMethod for checkout.
checkout_data = checkout_delivery_method_update(
e2e_not_logged_api_client,
checkout_id,
@@ -211,31 +842,926 @@ def test_checkout_calculate_discount_for_sale_and_voucher_1014(
)
assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
- total_gross_amount = round(subtotal_amount + shipping_price, 2)
- assert checkout_data["totalPrice"]["gross"]["amount"] == total_gross_amount
+ calculated_total = calculated_subtotal + shipping_price
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
- # Step 5 - Create payment for checkout.
+ # Step 7 - Create payment for checkout.
checkout_dummy_payment_create(
- e2e_not_logged_api_client, checkout_id, total_gross_amount
+ e2e_not_logged_api_client,
+ checkout_id,
+ total_gross_amount,
)
- # Step 6 - Complete checkout.
+ # Step 8 - Complete checkout.
order_data = checkout_complete(
e2e_not_logged_api_client,
checkout_id,
)
+ assert len(order_data["lines"]) == 3
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
+
assert order_data["discounts"][0]["type"] == "VOUCHER"
assert order_data["voucher"]["code"] == voucher_code
+ assert order_data["discounts"][0]["value"] == calculated_discount
- order_line = order_data["lines"][0]
- assert order_line["unitDiscountType"] == "FIXED"
- assert order_line["unitPrice"]["gross"]["amount"] == expected_unit_price
- assert order_line["unitDiscountReason"] == (
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+ assert order_line1["unitPrice"]["gross"]["amount"] == round(
+ calculated_line1_total / product2_new_quantity, 2
+ )
+ assert order_line1["unitDiscount"]["amount"] == round(
+ line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ )
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitDiscountReason"] == (
f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
)
- assert order_data["total"]["gross"]["amount"] == total_gross_amount
- assert order_data["subtotal"]["gross"]["amount"] == subtotal_amount
- assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
- product_variant_price
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
+ )
+ assert order_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+ assert order_line2["unitPrice"]["gross"]["amount"] == round(
+ calculated_line2_total / product3_new_quantity, 2
+ )
+ assert order_line2["unitDiscount"]["amount"] == round(
+ line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ )
+
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code}"
+ )
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
+ )
+ assert order_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ assert order_line3["unitPrice"]["gross"]["amount"] == round(
+ calculated_line3_total / product4_quantity, 2
+ )
+ assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+
+
+def test_checkout_calculate_discount_for_fixed_sale_and_percentage_voucher_CORE_0114(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+):
+ # Before
+ permissions = [
+ *shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+
+ products_data = prepare_products(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ ["13.33", "16", "9.99", "20.5"],
+ )
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
+
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
+
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+
+ sale_id, sale_discount_value = prepare_sale_for_products(
+ e2e_staff_api_client,
+ channel_id,
+ product_ids,
+ sale_discount_type="FIXED",
+ sale_discount_value=5,
+ sale_name="Sale_fixed",
+ )
+
+ # prices are updated in the background, we need to force it to retrieve the correct
+ # ones
+ recalculate_discounted_price_for_products_task()
+
+ voucher_discount_value, voucher_code = prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ "VOUCHER003",
+ voucher_discount_type="PERCENTAGE",
+ voucher_discount_value=15,
+ )
+ product1_quantity = 2
+ product2_quantity = 3
+ product3_quantity = 4
+ product4_quantity = 1
+ product2_new_quantity = 2
+ product3_new_quantity = 3
+
+ # Step 1 - checkoutCreate for product on sale
+ lines = [
+ {
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
+ },
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product1_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
+ )
+
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = product2_variant_price - line2_discount
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_not_logged_api_client,
+ checkout_id,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_discount = sale_discount_value
+ line3_unit_price = product3_variant_price - line3_discount
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == 1
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product2_variant_price - line1_discount
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ )
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == 1
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ +product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_discount = sale_discount_value
+ line1_unit_price = product2_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_line1_total = round(product2_new_quantity * line1_unit_price, 2)
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_discount = sale_discount_value
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ calculated_line2_total = round(product3_new_quantity * line2_unit_price, 2)
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_line3_total = round(product4_quantity * line3_unit_price, 2)
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ calculated_subtotal = round(
+ +product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ calculated_discount = round(calculated_subtotal * voucher_discount_value / 100, 2)
+ assert checkout_data["discount"]["amount"] == calculated_discount
+ assert checkout_data["voucherCode"] == voucher_code
+
+ # Assert lines after voucher code
+ assert len(checkout_data["lines"]) == 3
+ checkout_line1 = checkout_data["lines"][0]
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line1["quantity"] == product2_new_quantity
+
+ calculated_line1_discount = round(
+ calculated_line1_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line1_total = round(
+ calculated_line1_total - calculated_line1_discount, 2
+ )
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ checkout_line2 = checkout_data["lines"][1]
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line2["quantity"] == product3_new_quantity
+
+ calculated_line2_discount = round(
+ calculated_line2_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line2_total = round(
+ calculated_line2_total - calculated_line2_discount, 2
+ )
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product4_variant_price
+ assert checkout_line3["quantity"] == product4_quantity
+
+ calculated_line3_discount = round(
+ calculated_discount - calculated_line1_discount - calculated_line2_discount, 2
+ )
+ calculated_line3_total = round(
+ calculated_line3_total - calculated_line3_discount, 2
+ )
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+
+ # Assert total and subtotal after voucher code
+ calculated_subtotal = round(calculated_subtotal - calculated_discount, 2)
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+ calculated_total = calculated_subtotal
+ assert checkout_data["totalPrice"]["gross"]["amount"] == calculated_total
+
+ # Step 6 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
+
+ # Step 7 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ )
+
+ # Step 8 - Complete checkout.
+ order_data = checkout_complete(
+ e2e_not_logged_api_client,
+ checkout_id,
+ )
+ assert len(order_data["lines"]) == 3
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
+
+ assert order_data["discounts"][0]["type"] == "VOUCHER"
+ assert order_data["voucher"]["code"] == voucher_code
+ assert order_data["discounts"][0]["value"] == calculated_discount
+
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+ assert order_line1["unitPrice"]["gross"]["amount"] == round(
+ calculated_line1_total / product2_new_quantity, 2
+ )
+ assert order_line1["unitDiscount"]["amount"] == round(
+ line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ )
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
+ )
+ assert order_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+ assert order_line2["unitPrice"]["gross"]["amount"] == round(
+ calculated_line2_total / product3_new_quantity, 2
+ )
+ assert order_line2["unitDiscount"]["amount"] == round(
+ line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ )
+
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code}"
+ )
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
+ )
+ assert order_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ assert order_line3["unitPrice"]["gross"]["amount"] == round(
+ calculated_line3_total / product4_quantity, 2
+ )
+ assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
+
+
+def test_checkout_calculate_discount_for_percentage_sale_and_fixed_voucher_CORE_0114(
+ e2e_not_logged_api_client,
+ e2e_staff_api_client,
+ shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+):
+ # Before
+ permissions = [
+ *shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ channel_id = shop_data["channel"]["id"]
+ channel_slug = shop_data["channel"]["slug"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+
+ products_data = prepare_products(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ ["13.33", "16", "9.99", "20.5"],
+ )
+ product1_id = products_data[0]["product_id"]
+ product1_variant_id = products_data[0]["variant_id"]
+ product1_variant_price = float(products_data[0]["price"])
+
+ product2_id = products_data[1]["product_id"]
+ product2_variant_id = products_data[1]["variant_id"]
+ product2_variant_price = float(products_data[1]["price"])
+
+ product3_id = products_data[2]["product_id"]
+ product3_variant_id = products_data[2]["variant_id"]
+ product3_variant_price = float(products_data[2]["price"])
+
+ product4_variant_id = products_data[3]["variant_id"]
+ product4_variant_price = float(products_data[3]["price"])
+
+ product_ids = [product1_id, product2_id, product3_id]
+
+ sale_id, sale_discount_value = prepare_sale_for_products(
+ e2e_staff_api_client,
+ channel_id,
+ product_ids,
+ sale_discount_type="PERCENTAGE",
+ sale_discount_value=20,
+ sale_name="Sale_Percentage",
+ )
+
+ # prices are updated in the background, we need to force it to retrieve the correct
+ # ones
+ recalculate_discounted_price_for_products_task()
+
+ voucher_discount_value, voucher_code = prepare_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ "VOUCHER004",
+ voucher_discount_type="FIXED",
+ voucher_discount_value=3,
+ )
+ product1_quantity = 2
+ product2_quantity = 3
+ product3_quantity = 4
+ product4_quantity = 1
+ product2_new_quantity = 2
+ product3_new_quantity = 3
+
+ # Step 1 - checkoutCreate for product on sale
+ lines = [
+ {
+ "variantId": product1_variant_id,
+ "quantity": product1_quantity,
+ },
+ {
+ "variantId": product2_variant_id,
+ "quantity": product2_quantity,
+ },
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+
+ assert checkout_line1["variant"]["id"] == product1_variant_id
+ assert checkout_line1["quantity"] == product1_quantity
+ line1_discount = round(product1_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product1_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product1_variant_price
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product1_quantity, 2
+ )
+
+ assert checkout_line2["variant"]["id"] == product2_variant_id
+ assert checkout_line2["quantity"] == product2_quantity
+ line2_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = product2_variant_price - line2_discount
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product2_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price + product2_quantity * line2_unit_price, 2
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 2 - Add Lines
+ lines = [
+ {
+ "variantId": product3_variant_id,
+ "quantity": product3_quantity,
+ },
+ {
+ "variantId": product4_variant_id,
+ "quantity": product4_quantity,
+ },
+ ]
+ checkout_data = checkout_lines_add(
+ e2e_not_logged_api_client,
+ checkout_id,
+ lines,
+ )
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["variant"]["id"] == product3_variant_id
+ assert checkout_line3["quantity"] == product3_quantity
+ line3_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line3_unit_price = product3_variant_price - line3_discount
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product3_quantity, 2
+ )
+
+ checkout_line4 = checkout_data["lines"][3]
+ assert checkout_line4["variant"]["id"] == product4_variant_id
+ assert checkout_line4["quantity"] == 1
+ line4_unit_price = product4_variant_price
+ assert checkout_line4["unitPrice"]["gross"]["amount"] == line4_unit_price
+ assert checkout_line4["undiscountedUnitPrice"]["amount"] == line4_unit_price
+ assert checkout_line4["totalPrice"]["gross"]["amount"] == round(
+ line4_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ product1_quantity * line1_unit_price
+ + product2_quantity * line2_unit_price
+ + product3_quantity * line3_unit_price
+ + product4_quantity * line4_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 3 - Remove lines
+ checkout_data = checkout_lines_delete(
+ e2e_not_logged_api_client, checkout_id, linesIds=[checkout_line1["id"]]
+ )
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ new_checkout_line1 = checkout_data["lines"][0]
+ new_checkout_line2 = checkout_data["lines"][1]
+ new_checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert new_checkout_line1["variant"]["id"] == product2_variant_id
+ assert new_checkout_line1["quantity"] == product2_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert new_checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert (
+ new_checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ )
+ assert new_checkout_line1["totalPrice"]["gross"]["amount"] == round(
+ line1_unit_price * product2_quantity, 2
+ )
+
+ assert new_checkout_line2["variant"]["id"] == product3_variant_id
+ assert new_checkout_line2["quantity"] == product3_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert new_checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert (
+ new_checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ )
+ assert new_checkout_line2["totalPrice"]["gross"]["amount"] == round(
+ line2_unit_price * product3_quantity, 2
+ )
+
+ assert new_checkout_line3["variant"]["id"] == product4_variant_id
+ assert new_checkout_line3["quantity"] == 1
+ line3_unit_price = product4_variant_price
+ assert new_checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert new_checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ assert new_checkout_line3["totalPrice"]["gross"]["amount"] == round(
+ line3_unit_price * product4_quantity, 2
+ )
+
+ calculated_subtotal = round(
+ +product2_quantity * line1_unit_price
+ + product3_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 4 - Update lines
+ lines = [
+ {
+ "quantity": product2_new_quantity,
+ "lineId": new_checkout_line1["id"],
+ },
+ {
+ "quantity": product3_new_quantity,
+ "lineId": new_checkout_line2["id"],
+ },
+ ]
+ checkout_data = checkout_lines_update(e2e_not_logged_api_client, checkout_id, lines)
+ checkout_data = checkout_data["checkout"]
+ assert len(checkout_data["lines"]) == 3
+ assert checkout_data["lines"][0]["id"] == checkout_line2["id"]
+ assert checkout_data["lines"][1]["id"] == checkout_line3["id"]
+ assert checkout_data["lines"][2]["id"] == checkout_line4["id"]
+
+ checkout_line1 = checkout_data["lines"][0]
+ checkout_line2 = checkout_data["lines"][1]
+ checkout_line3 = checkout_data["lines"][2] # not on sale
+
+ assert checkout_line1["variant"]["id"] == product2_variant_id
+ assert checkout_line1["quantity"] == product2_new_quantity
+ line1_discount = round(product2_variant_price * sale_discount_value / 100, 2)
+ line1_unit_price = product2_variant_price - line1_discount
+ assert checkout_line1["unitPrice"]["gross"]["amount"] == line1_unit_price
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ calculated_line1_total = round(product2_new_quantity * line1_unit_price, 2)
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ assert checkout_line2["variant"]["id"] == product3_variant_id
+ assert checkout_line2["quantity"] == product3_new_quantity
+ line2_discount = round(product3_variant_price * sale_discount_value / 100, 2)
+ line2_unit_price = round(product3_variant_price - line2_discount, 2)
+ assert checkout_line2["unitPrice"]["gross"]["amount"] == line2_unit_price
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ calculated_line2_total = round(product3_new_quantity * line2_unit_price, 2)
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ assert checkout_line3["variant"]["id"] == product4_variant_id
+ assert checkout_line3["quantity"] == product4_quantity
+ line3_unit_price = product4_variant_price
+ assert checkout_line3["unitPrice"]["gross"]["amount"] == line3_unit_price
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == line3_unit_price
+ calculated_line3_total = round(product4_quantity * line3_unit_price, 2)
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ calculated_subtotal = round(
+ +product2_new_quantity * line1_unit_price
+ + product3_new_quantity * line2_unit_price
+ + product4_quantity * line3_unit_price,
+ 2,
+ )
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+
+ # Step 5 - Add voucher code to checkout
+ checkout_data = checkout_add_promo_code(
+ e2e_not_logged_api_client, checkout_id, voucher_code
+ )
+ calculated_discount = voucher_discount_value
+ assert checkout_data["discount"]["amount"] == calculated_discount
+ assert checkout_data["voucherCode"] == voucher_code
+
+ # Assert lines after voucher code
+ assert len(checkout_data["lines"]) == 3
+ checkout_line1 = checkout_data["lines"][0]
+ assert checkout_line1["undiscountedUnitPrice"]["amount"] == product2_variant_price
+ assert checkout_line1["quantity"] == product2_new_quantity
+
+ calculated_line1_discount = round(
+ calculated_line1_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line1_total = round(
+ calculated_line1_total - calculated_line1_discount, 2
+ )
+ assert checkout_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+
+ checkout_line2 = checkout_data["lines"][1]
+ assert checkout_line2["undiscountedUnitPrice"]["amount"] == product3_variant_price
+ assert checkout_line2["quantity"] == product3_new_quantity
+
+ calculated_line2_discount = round(
+ calculated_line2_total / calculated_subtotal * calculated_discount, 2
+ )
+ calculated_line2_total = round(
+ calculated_line2_total - calculated_line2_discount, 2
+ )
+ assert checkout_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+
+ checkout_line3 = checkout_data["lines"][2]
+ assert checkout_line3["undiscountedUnitPrice"]["amount"] == product4_variant_price
+ assert checkout_line3["quantity"] == product4_quantity
+
+ calculated_line3_discount = round(
+ calculated_discount - calculated_line1_discount - calculated_line2_discount, 2
+ )
+ calculated_line3_total = round(
+ calculated_line3_total - calculated_line3_discount, 2
+ )
+ assert checkout_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+
+ # Assert total and subtotal after voucher code
+ calculated_subtotal = round(calculated_subtotal - calculated_discount, 2)
+ assert checkout_data["subtotalPrice"]["gross"]["amount"] == calculated_subtotal
+ calculated_total = calculated_subtotal
+ assert checkout_data["totalPrice"]["gross"]["amount"] == calculated_total
+
+ # Step 6 - Set DeliveryMethod for checkout.
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]
+ calculated_total = calculated_subtotal + shipping_price
+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]
+ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]
+ assert total_gross_amount == calculated_total
+ assert subtotal_gross_amount == calculated_subtotal
+
+ # Step 7 - Create payment for checkout.
+ checkout_dummy_payment_create(
+ e2e_not_logged_api_client,
+ checkout_id,
+ total_gross_amount,
+ )
+
+ # Step 8 - Complete checkout.
+ order_data = checkout_complete(
+ e2e_not_logged_api_client,
+ checkout_id,
+ )
+ assert len(order_data["lines"]) == 3
+ order_line1 = order_data["lines"][0]
+ order_line2 = order_data["lines"][1]
+ order_line3 = order_data["lines"][2]
+ assert order_data["status"] == "UNFULFILLED"
+ assert order_data["total"]["gross"]["amount"] == calculated_total
+ assert order_data["subtotal"]["gross"]["amount"] == calculated_subtotal
+ received_subtotal_from_lines = round(
+ order_line1["totalPrice"]["gross"]["amount"]
+ + order_line2["totalPrice"]["gross"]["amount"]
+ + order_line3["totalPrice"]["gross"]["amount"],
+ 2,
+ )
+
+ assert order_data["subtotal"]["gross"]["amount"] == received_subtotal_from_lines
+ assert order_data["shippingPrice"]["gross"]["amount"] == shipping_price
+
+ assert order_data["discounts"][0]["type"] == "VOUCHER"
+ assert order_data["voucher"]["code"] == voucher_code
+ assert order_data["discounts"][0]["value"] == calculated_discount
+
+ assert order_line1["quantity"] == product2_new_quantity
+ assert order_line1["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line1["undiscountedUnitPrice"]["gross"]["amount"]
+ == product2_variant_price
+ )
+ assert order_line1["totalPrice"]["gross"]["amount"] == calculated_line1_total
+ assert order_line1["unitPrice"]["gross"]["amount"] == round(
+ calculated_line1_total / product2_new_quantity, 2
+ )
+ assert order_line1["unitDiscount"]["amount"] == round(
+ line1_discount + (calculated_line1_discount / product2_new_quantity), 2
+ )
+ assert order_line2["quantity"] == product3_new_quantity
+ assert order_line2["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
+ )
+ assert (
+ order_line2["undiscountedUnitPrice"]["gross"]["amount"]
+ == product3_variant_price
+ )
+ assert order_line2["totalPrice"]["gross"]["amount"] == calculated_line2_total
+ assert order_line2["unitPrice"]["gross"]["amount"] == round(
+ calculated_line2_total / product3_new_quantity, 2
+ )
+ assert order_line2["unitDiscount"]["amount"] == round(
+ line2_discount + (calculated_line2_discount / product3_new_quantity), 2
+ )
+
+ assert order_line3["quantity"] == product4_quantity
+ assert order_line3["unitDiscountReason"] == (
+ f"Entire order voucher code: {voucher_code}"
+ )
+ assert (
+ order_line3["undiscountedUnitPrice"]["gross"]["amount"]
+ == product4_variant_price
+ )
+ assert order_line3["totalPrice"]["gross"]["amount"] == calculated_line3_total
+ assert order_line3["unitPrice"]["gross"]["amount"] == round(
+ calculated_line3_total / product4_quantity, 2
)
+ assert order_line3["unitDiscount"]["amount"] == calculated_line3_discount
diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.py
index 4a47f8511a..ae52b2a190 100644
--- a/saleor/tests/e2e/checkout/utils/__init__.py
+++ b/saleor/tests/e2e/checkout/utils/__init__.py
@@ -12,6 +12,7 @@ from .checkout_create_from_order import checkout_create_from_order
from .checkout_delivery_method_update import checkout_delivery_method_update
from .checkout_email_update import checkout_update_email
from .checkout_lines_add import checkout_lines_add
+from .checkout_lines_delete import checkout_lines_delete
from .checkout_lines_update import checkout_lines_update
from .checkout_payment_create import (
checkout_dummy_payment_create,
@@ -42,4 +43,5 @@ __all__ = [
"get_checkout",
"checkout_remove_promo_code",
"checkout_update_email",
+ "checkout_lines_delete",
]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
index f18d9c60b7..d4bed13368 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_add_promo_code.py
@@ -36,6 +36,8 @@ mutation AddCheckoutPromoCode($checkoutId: ID!, $promoCode: String!) {
}
discountName
lines {
+ id
+ quantity
totalPrice {
gross {
amount
diff --git a/saleor/tests/e2e/checkout/utils/checkout_complete.py b/saleor/tests/e2e/checkout/utils/checkout_complete.py
index b1d0c4b341..37c07641ad 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_complete.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_complete.py
@@ -60,6 +60,7 @@ mutation CheckoutComplete($checkoutId: ID!) {
}
lines {
id
+ quantity
unitPrice {
gross {
amount
@@ -76,6 +77,11 @@ mutation CheckoutComplete($checkoutId: ID!) {
amount
}
}
+ totalPrice {
+ gross {
+ amount
+ }
+ }
}
discounts {
id
diff --git a/saleor/tests/e2e/checkout/utils/checkout_create.py b/saleor/tests/e2e/checkout/utils/checkout_create.py
index d45440bb05..2a254726f5 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_create.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_create.py
@@ -83,6 +83,7 @@ mutation CreateCheckout($input: CheckoutCreateInput!) {
}
lines {
id
+ quantity
totalPrice {
gross {
amount
diff --git a/saleor/tests/e2e/checkout/utils/checkout_lines_add.py b/saleor/tests/e2e/checkout/utils/checkout_lines_add.py
index d7e26cd78c..4836f744fc 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_lines_add.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_lines_add.py
@@ -5,7 +5,11 @@ mutation checkoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
checkoutLinesAdd(id: $checkoutId, lines: $lines) {
checkout {
lines {
+ id
quantity
+ variant {
+ id
+ }
totalPrice {
gross {
amount
@@ -23,6 +27,11 @@ mutation checkoutLinesAdd($checkoutId: ID!, $lines: [CheckoutLineInput!]!) {
availablePaymentGateways {
id
}
+ subtotalPrice {
+ gross {
+ amount
+ }
+ }
}
errors {
field
diff --git a/saleor/tests/e2e/checkout/utils/checkout_lines_delete.py b/saleor/tests/e2e/checkout/utils/checkout_lines_delete.py
new file mode 100644
index 0000000000..2d47df6f2e
--- /dev/null
+++ b/saleor/tests/e2e/checkout/utils/checkout_lines_delete.py
@@ -0,0 +1,72 @@
+from ...utils import get_graphql_content
+
+CHECKOUT_LINES_DELETE_MUTATION = """
+mutation CheckoutLinesDelete($checkoutId: ID!, $linesIds: [ID!]!) {
+ checkoutLinesDelete(id: $checkoutId, linesIds: $linesIds) {
+ checkout {
+ discountName
+ discount {
+ amount
+ }
+ subtotalPrice {
+ gross {
+ amount
+ }
+ }
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ lines {
+ id
+ quantity
+ undiscountedTotalPrice {
+ amount
+ }
+ variant {
+ id
+ quantityLimitPerCustomer
+ }
+ unitPrice {
+ gross {
+ amount
+ }
+ }
+ undiscountedUnitPrice {
+ amount
+ }
+ totalPrice {
+ gross {
+ amount
+ }
+ }
+ }
+ }
+ errors {
+ field
+ lines
+ message
+ variants
+ code
+ }
+ }
+}
+"""
+
+
+def checkout_lines_delete(
+ staff_api_client,
+ checkout_id,
+ linesIds,
+):
+ variables = {
+ "checkoutId": checkout_id,
+ "linesIds": linesIds,
+ }
+
+ response = staff_api_client.post_graphql(CHECKOUT_LINES_DELETE_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ assert content["data"]["checkoutLinesDelete"]["errors"] == []
+ return content["data"]["checkoutLinesDelete"]["checkout"]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_lines_update.py b/saleor/tests/e2e/checkout/utils/checkout_lines_update.py
index 3fd8514e0c..60482fb44f 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_lines_update.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_lines_update.py
@@ -19,6 +19,7 @@ mutation checkoutLinesUpdate($checkoutId: ID!, $lines: [CheckoutLineUpdateInput!
}
}
lines {
+ id
quantity
undiscountedTotalPrice {
amount
diff --git a/saleor/tests/e2e/product/utils/preparing_product.py b/saleor/tests/e2e/product/utils/preparing_product.py
index 6dd0074bb9..a02a9fa042 100644
--- a/saleor/tests/e2e/product/utils/preparing_product.py
+++ b/saleor/tests/e2e/product/utils/preparing_product.py
@@ -114,3 +114,29 @@ def prepare_digital_product(
product_variant_price = variant_listing["channelListings"][0]["price"]["amount"]
return product_id, product_variant_id, product_variant_price
+
+
+def prepare_products(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ prices,
+):
+ products_data = []
+
+ for i, variant_price in enumerate(prices):
+ product_id, variant_id, price = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price,
+ product_type_slug=f"test-{i}",
+ )
+ product_data = {
+ "product_id": product_id,
+ "variant_id": variant_id,
+ "price": price,
+ }
+ products_data.append(product_data)
+
+ return products_data