Skip to content

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

Fix available shipping method price rules (#6643)

Filip Owczarek06aee553592a
diff --git a/saleor/shipping/models.py b/saleor/shipping/models.py
index bf64f31881..cc999c1b90 100644
--- a/saleor/shipping/models.py
+++ b/saleor/shipping/models.py
@@ -37,17 +37,21 @@ def _applicable_weight_based_methods(weight, qs):
return qs.filter(min_weight_matched & (no_weight_limit | max_weight_matched))
-def _applicable_price_based_methods(price: Money, qs):
+def _applicable_price_based_methods(price: Money, qs, channel_id):
"""Return price based shipping methods that are applicable for the given total."""
qs_shipping_method = qs.price_based()
price_based = Q(shipping_method_id__in=qs_shipping_method)
+ channel_filter = Q(channel_id=channel_id)
min_price_matched = Q(minimum_order_price_amount__lte=price.amount)
no_price_limit = Q(maximum_order_price_amount__isnull=True)
max_price_matched = Q(maximum_order_price_amount__gte=price.amount)
applicable_price_based_methods = ShippingMethodChannelListing.objects.filter(
- price_based & min_price_matched & (no_price_limit | max_price_matched)
+ channel_filter
+ & price_based
+ & min_price_matched
+ & (no_price_limit | max_price_matched)
).values_list("shipping_method__id", flat=True)
return qs_shipping_method.filter(id__in=applicable_price_based_methods)
@@ -126,7 +130,7 @@ class ShippingMethodQueryset(models.QuerySet):
if product_ids:
qs = self.exclude_shipping_methods_for_excluded_products(qs, product_ids)
- price_based_methods = _applicable_price_based_methods(price, qs)
+ price_based_methods = _applicable_price_based_methods(price, qs, channel_id)
weight_based_methods = _applicable_weight_based_methods(weight, qs)
shipping_methods = price_based_methods | weight_based_methods
diff --git a/saleor/shipping/tests/test_shipping.py b/saleor/shipping/tests/test_shipping.py
index 52c0df4ab1..19ccc7004b 100644
--- a/saleor/shipping/tests/test_shipping.py
+++ b/saleor/shipping/tests/test_shipping.py
@@ -290,3 +290,90 @@ def test_default_shipping_zone_exists(shipping_zone):
def test_get_countries_without_shipping_zone(shipping_zone_without_countries):
countries_no_shipping_zone = set(get_countries_without_shipping_zone())
assert {c.code for c in countries} == countries_no_shipping_zone
+
+
+def test_applicable_shipping_methods_price_rate_use_proper_channel(
+ shipping_zone, channel_USD, other_channel_USD
+):
+ # given
+ # Price method with different min and max in channels to total to low to apply
+ price_method_1 = shipping_zone.shipping_methods.create(
+ type=ShippingMethodType.PRICE_BASED,
+ )
+ # Price method with different min and max in channels total correct to apply
+ price_method_2 = shipping_zone.shipping_methods.create(
+ type=ShippingMethodType.PRICE_BASED,
+ )
+ # Price method with different min and max in channels total to hight to apply
+ price_method_3 = shipping_zone.shipping_methods.create(
+ type=ShippingMethodType.PRICE_BASED,
+ )
+ # Price method not assigned to channel
+ price_method_4 = shipping_zone.shipping_methods.create(
+ type=ShippingMethodType.PRICE_BASED,
+ )
+
+ ShippingMethodChannelListing.objects.bulk_create(
+ [
+ # price_method_1
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("10.0", "USD"),
+ maximum_order_price=Money("100.0", "USD"),
+ shipping_method=price_method_1,
+ channel=channel_USD,
+ ),
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("1.0", "USD"),
+ maximum_order_price=Money("100.0", "USD"),
+ shipping_method=price_method_1,
+ channel=other_channel_USD,
+ ),
+ # price_method_2
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("4.0", "USD"),
+ maximum_order_price=Money("10.0", "USD"),
+ shipping_method=price_method_2,
+ channel=channel_USD,
+ ),
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("1.0", "USD"),
+ maximum_order_price=Money("100.0", "USD"),
+ shipping_method=price_method_2,
+ channel=other_channel_USD,
+ ),
+ # price_method_3
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("1.0", "USD"),
+ maximum_order_price=Money("4.0", "USD"),
+ shipping_method=price_method_3,
+ channel=channel_USD,
+ ),
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("1.0", "USD"),
+ maximum_order_price=Money("100.0", "USD"),
+ shipping_method=price_method_3,
+ channel=other_channel_USD,
+ ),
+ # price_method_4
+ ShippingMethodChannelListing(
+ minimum_order_price=Money("1.0", "USD"),
+ maximum_order_price=Money("100.0", "USD"),
+ shipping_method=price_method_4,
+ channel=other_channel_USD,
+ ),
+ ]
+ )
+
+ # when
+ result = ShippingMethod.objects.applicable_shipping_methods(
+ price=Money("5.0", "USD"),
+ weight=Weight(kg=5),
+ country_code="PL",
+ channel_id=channel_USD.id,
+ )
+
+ # then
+ assert price_method_1 not in result
+ assert price_method_3 not in result
+ assert price_method_4 not in result
+ assert price_method_2 in result