Skip to content

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

Update schema in front page and search results

Krzysztof Wolskie3b57c928520
diff --git a/saleor/core/templatetags/jsonld.py b/saleor/core/templatetags/jsonld.py
new file mode 100644
index 0000000000..f312555ee2
--- /dev/null
+++ b/saleor/core/templatetags/jsonld.py
@@ -0,0 +1,14 @@
+from __future__ import unicode_literals
+
+import json
+
+from django.template import Library
+
+from saleor.product.utils import product_json_ld
+
+register = Library()
+
+
+def product_availability_schema(product):
+ return json.dumps(product_json_ld(product))
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index 180223997c..8f80815ae9 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -120,7 +120,7 @@ def products_for_cart(user):
return products
-def product_json_ld(product, availability, attributes):
+def product_json_ld(product, availability=None, attributes=None):
# type: (saleor.product.models.Product, saleor.product.utils.ProductAvailability, dict) -> dict # noqa
"""Generates JSON-LD data for product"""
data = {'@context': 'http://schema.org/',
@@ -131,25 +131,27 @@ def product_json_ld(product, availability, attributes):
'offers': {'@type': 'Offer',
'itemCondition': 'http://schema.org/NewCondition'}}
- if availability.price_range:
- data['offers']['priceCurrency'] = settings.DEFAULT_CURRENCY
- data['offers']['price'] = availability.price_range.min_price.net
+ if availability is not None:
+ if availability.price_range:
+ data['offers']['priceCurrency'] = settings.DEFAULT_CURRENCY
+ data['offers']['price'] = availability.price_range.min_price.net
- if availability.available:
- data['offers']['availability'] = 'http://schema.org/InStock'
- else:
- data['offers']['availability'] = 'http://schema.org/OutOfStock'
-
- brand = ''
- for key in attributes:
- if key.name == 'brand':
- brand = attributes[key].display
- break
- elif key.name == 'publisher':
- brand = attributes[key].display
-
- if brand:
- data['brand'] = {'@type': 'Thing', 'name': brand}
+ if availability.available:
+ data['offers']['availability'] = 'http://schema.org/InStock'
+ else:
+ data['offers']['availability'] = 'http://schema.org/OutOfStock'
+
+ if attributes is not None:
+ brand = ''
+ for key in attributes:
+ if key.name == 'brand':
+ brand = attributes[key].display
+ break
+ elif key.name == 'publisher':
+ brand = attributes[key].display
+
+ if brand:
+ data['brand'] = {'@type': 'Thing', 'name': brand}
return data
diff --git a/templates/category/_items.html b/templates/category/_items.html
index 71aa910dd8..a25eb12467 100644
--- a/templates/category/_items.html
+++ b/templates/category/_items.html
@@ -2,14 +2,18 @@
{% load staticfiles %}
{% load price_range from price_ranges %}
{% load product_first_image from product_images %}
+{% load product_availability_schema from jsonld %}
{% for product, availability in products %}
- <div class="col-6 col-lg-3 product-list" itemscope itemtype="https://schema.org/Product">
- <a itemprop="url" href="{{ product.get_absolute_url }}">
+ <div class="col-6 col-lg-3 product-list">
+ <script type="application/ld+json">
+ {% autoescape off %}{% product_availability_schema product %}{% endautoescape %}
+ </script>
+ <a href="{{ product.get_absolute_url }}">
<div class="text-center">
<div>
- <img itemprop="image" class="img-responsive" src="{% product_first_image product method="crop" size="400x400" %}" alt="">
- <span class="product-list-item-name" itemprop="name" title="{{ product }}">{{ product }}</span>
+ <img class="img-responsive" src="{% product_first_image product method="crop" size="400x400" %}" alt="">
+ <span class="product-list-item-name" title="{{ product }}">{{ product }}</span>
</div>
<div class="panel-footer">