Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix #1011
Marcin Gębala049ff416360d
diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 0584ca3b48..13abcca068 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -13,7 +13,7 @@ from django.utils.encoding import python_2_unicode_compatible, smart_textfrom django.utils.text import slugifyfrom django.utils.translation import pgettext_lazyfrom django.utils import six-from django_prices.models import PriceField+from django_prices.models import Price, PriceFieldfrom mptt.managers import TreeManagerfrom mptt.models import MPTTModelfrom prices import PriceRange@@ -195,7 +195,7 @@ class Product(models.Model, ItemRange, index.Indexed):def set_attribute(self, pk, value_pk):self.attributes[smart_text(pk)] = smart_text(value_pk)- def get_price_range(self, discounts=None, **kwargs):+ def get_price_range(self, discounts=None, **kwargs):if not self.variants.exists():price = calculate_discounted_price(self, self.price, discounts, **kwargs)@@ -300,11 +300,14 @@ class ProductVariant(models.Model, Item):return self.product.get_first_image()def select_stockrecord(self, quantity=1):- # By default selects stock with lowest cost price- stock = filter(- lambda stock: stock.quantity_available >= quantity,- self.stock.all())- stock = sorted(stock, key=lambda stock: stock.cost_price, reverse=True)+ # By default selects stock with lowest cost price. If stock cost price+ # is None we assume price equal to zero to allow sorting.+ stock = [+ stock_item for stock_item in self.stock.all()+ if stock_item.quantity_available >= quantity]+ zero_price = Price(0, currency=settings.DEFAULT_CURRENCY)+ stock = sorted(+ stock, key=(lambda s: s.cost_price or zero_price), reverse=False)if stock:return stock[0]