Skip to content

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

Merge branch 'master' into avatax_integration

Maciej Korycinskie1c9f38a62d7
diff --cc saleor/checkout/utils.py
index f9cd1509bc,0678355248..aabeb91b84
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@@ -101,7 -94,40 +101,46 @@@ def remove_unavailable_variants(checkou
add_variant_to_checkout(checkout, line.variant, quantity, replace=True)
-def get_variant_prices_from_lines(lines):
- """Get's price of each individual item within the lines."""
- return [line.variant.get_price() for line in lines for item in range(line.quantity)]
-
-
+ def get_prices_of_discounted_specific_product(lines, voucher):
+ """Get prices of variants belonging to the discounted specific products.
+
+ Specific products are products, collections and categories.
+ Product must be assigned directly to the discounted category, assigning
+ product to child category won't work.
+ """
- # If there's no discounted products, collections or categories,
- # it means that all products are discounted
+ discounted_products = voucher.products.all()
+ discounted_categories = set(voucher.categories.all())
+ discounted_collections = set(voucher.collections.all())
++
++ line_prices = []
++ discounted_lines = []
+ if discounted_products or discounted_collections or discounted_categories:
- discounted_lines = []
+ for line in lines:
+ line_product = line.variant.product
+ line_category = line.variant.product.category
+ line_collections = set(line.variant.product.collections.all())
+ if line.variant and (
+ line_product in discounted_products
+ or line_category in discounted_categories
+ or line_collections.intersection(discounted_collections)
+ ):
+ discounted_lines.append(line)
- return get_variant_prices_from_lines(discounted_lines)
- return get_variant_prices_from_lines(lines)
++ else:
++ # If there's no discounted products, collections or categories,
++ # it means that all products are discounted
++ discounted_lines.extend(list(lines))
++
++ for line in discounted_lines:
++ line_total = calculate_checkout_line_total(line, []).gross
++ line_unit_price = quantize_price(
++ (line_total / line.quantity), line_total.currency
++ )
++ line_prices.extend([line_unit_price] * line.quantity)
++
++ return line_prices
+
+
-def get_prices_of_discounted_products(lines, discounted_products):
+def get_prices_of_discounted_products(checkout, discounted_products):
"""Get prices of variants belonging to the discounted products."""
# If there's no discounted_products,
# it means that all products are discounted
@@@ -731,15 -740,19 +770,17 @@@ def _get_shipping_voucher_discount_for_
)
-def _get_products_voucher_discount(order_or_checkout, voucher):
- """Calculate products discount value for a voucher, depending on its type."""
+def _get_products_voucher_discount(checkout, voucher):
+ """Calculate products discount value for a voucher, depending on its type.
+ """
+ prices = None
- if voucher.type == VoucherType.PRODUCT:
+ if voucher.type == VoucherType.SPECIFIC_PRODUCT:
- prices = get_prices_of_discounted_specific_product(
- order_or_checkout.lines.all(), voucher
- )
++ prices = get_prices_of_discounted_specific_product(checkout, voucher)
+ elif voucher.type == VoucherType.PRODUCT:
- prices = get_prices_of_discounted_products(
- order_or_checkout.lines.all(), voucher.products.all()
- )
+ prices = get_prices_of_discounted_products(checkout, voucher.products.all())
elif voucher.type == VoucherType.COLLECTION:
prices = get_prices_of_products_in_discounted_collections(
- order_or_checkout.lines.all(), voucher.collections.all()
+ checkout, voucher.collections.all()
)
elif voucher.type == VoucherType.CATEGORY:
prices = get_prices_of_products_in_discounted_categories(
@@@ -758,12 -771,14 +799,12 @@@ def get_voucher_discount_for_checkout(v
Raise NotApplicable if voucher of given type cannot be applied.
"""
- if voucher.type == VoucherType.VALUE:
+ if voucher.type == VoucherType.ENTIRE_ORDER:
return get_value_voucher_discount(
- voucher, checkout.get_subtotal(discounts, taxes)
+ voucher, calculate_checkout_subtotal(checkout, discounts).gross
)
if voucher.type == VoucherType.SHIPPING:
- return _get_shipping_voucher_discount_for_checkout(
- voucher, checkout, discounts, taxes
- )
+ return _get_shipping_voucher_discount_for_checkout(voucher, checkout, discounts)
if voucher.type in (
VoucherType.PRODUCT,
VoucherType.COLLECTION,
diff --cc saleor/dashboard/order/utils.py
index 4aa1dd92c1,8763987834..05e3999bf9
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@@ -149,20 -79,20 +149,21 @@@ def get_voucher_discount_for_order(orde
Raise NotApplicable if voucher of given type cannot be applied.
"""
if not order.voucher:
- return ZERO_MONEY
+ return zero_money()
+ subtotal = order.get_subtotal()
- if order.voucher.type == VoucherType.VALUE:
+ if order.voucher.type == VoucherType.ENTIRE_ORDER:
- return get_value_voucher_discount(order.voucher, order.get_subtotal())
+ return get_value_voucher_discount(order.voucher, subtotal.gross)
if order.voucher.type == VoucherType.SHIPPING:
return get_shipping_voucher_discount(
- order.voucher, order.get_subtotal(), order.shipping_price
+ order.voucher, subtotal.gross, order.shipping_price
)
if order.voucher.type in (
VoucherType.PRODUCT,
VoucherType.COLLECTION,
VoucherType.CATEGORY,
+ VoucherType.SPECIFIC_PRODUCT,
):
- return _get_products_voucher_discount(order, order.voucher)
+ return get_products_voucher_discount_for_order(order, order.voucher)
raise NotImplementedError("Unknown discount type")
diff --cc tests/test_cart.py
index 786998965e,9563c4d237..1d53db8e6b
--- a/tests/test_cart.py
+++ b/tests/test_cart.py
@@@ -19,7 -19,9 +19,8 @@@ from saleor.checkout.utils import
)
from saleor.checkout.views import clear_checkout, update_checkout_line
from saleor.core.exceptions import InsufficientStock
-from saleor.core.utils.taxes import ZERO_TAXED_MONEY
from saleor.discount.models import Sale
+ from saleor.product.models import Category
from saleor.shipping.utils import get_shipping_price_estimate