Skip to content

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

Fix Schema.org handling

Patryk Zawadzki413ec31a1280
diff --git a/saleor/core/templatetags/jsonld.py b/saleor/core/templatetags/jsonld.py
deleted file mode 100644
index 7296ae6ac2..0000000000
--- a/saleor/core/templatetags/jsonld.py
+++ /dev/null
@@ -1,12 +0,0 @@
-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 18267fa24b..c711507cfc 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -109,26 +109,31 @@ def products_for_cart(user):
return products
-def product_json_ld(product, availability=None, attributes=None):
+def product_json_ld(product, 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/',
'@type': 'Product',
'name': smart_text(product),
- 'image': smart_text(product.get_first_image()),
+ 'image': [
+ product_image.image.url
+ for product_image in product.images.all()],
'description': product.description,
- 'offers': {'@type': 'Offer',
- 'itemCondition': 'http://schema.org/NewCondition'}}
+ 'offers': []}
- 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'
+ for variant in product.variants.all():
+ price = variant.get_price_per_item()
+ available = 'http://schema.org/InStock'
+ if not product.is_available() or not variant.is_in_stock():
+ available = 'http://schema.org/OutOfStock'
+ variant_data = {
+ '@type': 'Offer',
+ 'availability': available,
+ 'itemCondition': 'http://schema.org/NewCondition',
+ 'price': price.gross,
+ 'priceCurrency': price.currency,
+ 'sku': variant.sku}
+ data['offers'].append(variant_data)
if attributes is not None:
brand = ''
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 485326b7ab..47bc079408 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -64,7 +64,7 @@ def product_details(request, slug, product_id, form=None):
product, request.discounts, request.currency)
product_attributes = get_product_attributes_data(product)
show_variant_picker = all([v.attributes for v in product.variants.all()])
- json_ld_data = product_json_ld(product, availability, product_attributes)
+ json_ld_data = product_json_ld(product, product_attributes)
return TemplateResponse(
request, 'product/details.html',
{'is_visible': is_visible,
diff --git a/saleor/static/js/components/product-schema.js b/saleor/static/js/components/product-schema.js
deleted file mode 100644
index ee2e58fcb7..0000000000
--- a/saleor/static/js/components/product-schema.js
+++ /dev/null
@@ -1,19 +0,0 @@
-import React from 'react';
-import ReactDOM from 'react-dom';
-
-import ProductSchema from './variantPicker/ProductSchema';
-import variantPickerStore from '../stores/variantPicker';
-
-export default $(document).ready((e) => {
- const productSchemaContainer = document.getElementById('product-schema-component');
- if (productSchemaContainer) {
- let productSchema = JSON.parse(document.getElementById('product-schema-component').children[0].text);
- ReactDOM.render(
- <ProductSchema
- variantStore={variantPickerStore}
- productSchema={productSchema}
- />,
- productSchemaContainer
- );
- }
-});
diff --git a/saleor/static/js/components/variantPicker/ProductSchema.js b/saleor/static/js/components/variantPicker/ProductSchema.js
deleted file mode 100644
index 586e246814..0000000000
--- a/saleor/static/js/components/variantPicker/ProductSchema.js
+++ /dev/null
@@ -1,31 +0,0 @@
-import $ from 'jquery';
-import { observer } from 'mobx-react';
-import React, { Component, PropTypes } from 'react';
-
-@observer
-export default class ProductSchema extends Component {
-
- static propTypes = {
- variantStore: PropTypes.object.isRequired,
- productSchema: PropTypes.object
- };
-
- getSchema = () => {
- const { variantStore, productSchema } = this.props;
- let variant = variantStore.variant;
- if (!variantStore.isEmpty){
- let variantSchema = $.extend({}, productSchema);
- variantSchema['offers'] = variant.schemaData;
- return JSON.stringify(variantSchema);
- }else{
- return JSON.stringify(productSchema);
- }
- };
-
- render() {
- let body = this.getSchema();
- return (
- <script type="application/ld+json">{ body }</script>
- )
- }
-}
diff --git a/saleor/static/js/storefront.js b/saleor/static/js/storefront.js
index a56f7fc6b6..6813ee14e3 100644
--- a/saleor/static/js/storefront.js
+++ b/saleor/static/js/storefront.js
@@ -7,7 +7,6 @@ import './components/sorter';
import './components/variant-picker';
import './components/footer';
import './components/product-filters';
-import './components/product-schema';
import './components/address-form';
import './components/password-input';
import './components/styleguide';
diff --git a/templates/base.html b/templates/base.html
index 2725a57b6b..5303546e6b 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -276,6 +276,5 @@
{% render_bundle 'vendor' 'js' %}
{% render_bundle 'storefront' 'js' %}
{% endblock footer_scripts %}
-<script type="application/ld+json">{{ webpage_schema|safe }}</script>
</body>
</html>
diff --git a/templates/category/_items.html b/templates/category/_items.html
index 56ccbf8fae..4321284a42 100644
--- a/templates/category/_items.html
+++ b/templates/category/_items.html
@@ -2,14 +2,10 @@
{% load staticfiles %}
{% load price_range from price_ranges %}
{% load product_first_image from product_images %}
-{% load product_availability_schema from jsonld %}
{% load get_thumbnail from product_images %}
{% for product, availability in products %}
<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 }}" class="link--clean">
<div class="text-center">
<div>
diff --git a/templates/home.html b/templates/home.html
index ecc70f606b..413743f0c7 100644
--- a/templates/home.html
+++ b/templates/home.html
@@ -55,4 +55,5 @@
</div>
</div>
+ <script type="application/ld+json">{{ webpage_schema|safe }}</script>
{% endblock %}
diff --git a/templates/product/details.html b/templates/product/details.html
index 87ff3a64d3..a8ca17d390 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -55,9 +55,7 @@
</div>
{% endif %}
<div class="row product">
- <div id="product-schema-component">
- <script type="application/ld+json">{{ json_ld_product_data|safe }}</script>
- </div>
+ <script type="application/ld+json">{{ json_ld_product_data|safe }}</script>
<div class="col-md-6 col-12 product__gallery">
{% with images=product_images %}
{% if images %}