Skip to content

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

Override get_price_range method in product

Mira26dffdf11ed7
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index ee28442672..56f617f0ef 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -16,6 +16,7 @@ from django.utils import six
from django_prices.models import PriceField
from mptt.managers import TreeManager
from mptt.models import MPTTModel
+from prices import PriceRange
from satchless.item import InsufficientStock, Item, ItemRange
from text_unidecode import unidecode
@@ -201,6 +202,12 @@ 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, **kwargs):
+ if not self.variants.exists():
+ return PriceRange(self.price, self.price)
+ else:
+ return super(Product, self).get_price_range(**kwargs)
+
@python_2_unicode_compatible
class ProductVariant(models.Model, Item):
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index cf128249fe..59911f341e 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -55,15 +55,6 @@ ProductAvailability = namedtuple(
def get_availability(product, discounts=None, local_currency=None):
- if not product.variants.exists():
- return ProductAvailability(
- available=False,
- price_range=None,
- price_range_undiscounted=None,
- discount=None,
- price_range_local_currency=None,
- discount_local_currency=None)
-
# In default currency
price_range = product.get_price_range(discounts=discounts)
undiscounted = product.get_price_range()
diff --git a/saleor/static/js/components/categoryPage/ProductPrice.js b/saleor/static/js/components/categoryPage/ProductPrice.js
index 4c5fdceac4..c420e19f13 100644
--- a/saleor/static/js/components/categoryPage/ProductPrice.js
+++ b/saleor/static/js/components/categoryPage/ProductPrice.js
@@ -6,11 +6,10 @@ import SaleImg from '../../../images/sale_bg.svg';
const ProductPrice = ({ availability, price }) => {
const { discount, priceRange } = availability;
const isPriceRange = priceRange && priceRange.minPrice.gross !== priceRange.maxPrice.gross;
- const priceLocalized = priceRange ? priceRange.minPrice.grossLocalized : price.grossLocalized;
return (
<div>
<span itemProp="price">
- {isPriceRange && <span>{gettext('from')} </span>} {priceLocalized}
+ {isPriceRange && <span>{gettext('from')} </span>} {priceRange.minPrice.grossLocalized}
</span>
{discount && (
<div className="product-list__sale"><img src={SaleImg}/><span>{gettext('Sale')}</span></div>