Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Product schema updates according to chosen variant
Krzysztof Wolskid867e65d020e
diff --git a/saleor/product/utils.py b/saleor/product/utils.pyindex 56b0718349..180223997c 100644--- a/saleor/product/utils.py+++ b/saleor/product/utils.py@@ -120,6 +120,39 @@ def products_for_cart(user):return products+def product_json_ld(product, availability, attributes):+ # 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()),+ 'description': product.description,+ '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.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}+ return data++def get_variant_picker_data(product, discounts=None, local_currency=None):availability = get_availability(product, discounts, local_currency)variants = product.variants.all()@@ -141,12 +174,24 @@ def get_variant_picker_data(product, discounts=None, local_currency=None):price_local_currency = to_local_currency(price, local_currency)else:price_local_currency = None++ schema_data = {'@type': 'Offer',+ 'itemCondition': 'http://schema.org/NewCondition',+ 'priceCurrency': price.currency,+ 'price': price.net}++ if variant.is_in_stock():+ schema_data['availability'] = 'http://schema.org/InStock'+ else:+ schema_data['availability'] = 'http://schema.org/OutOfStock'+variant_data = {'id': variant.id,'price': price_as_dict(price),'priceUndiscounted': price_as_dict(price_undiscounted),'attributes': variant.attributes,- 'priceLocalCurrency': price_as_dict(price_local_currency)}+ 'priceLocalCurrency': price_as_dict(price_local_currency),+ 'schemaData': schema_data}data['variants'].append(variant_data)data['availability'] = {@@ -182,36 +227,3 @@ def price_range_as_dict(price_range):return Nonereturn {'maxPrice': price_as_dict(price_range.max_price),'minPrice': price_as_dict(price_range.min_price)}---def product_json_ld(product, availability, attributes):- # 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()),- 'description': product.description,- '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.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}- return datadiff --git a/saleor/static/js/components/variantPicker/ProductSchema.js b/saleor/static/js/components/variantPicker/ProductSchema.jsnew file mode 100644index 0000000000..586e246814--- /dev/null+++ b/saleor/static/js/components/variantPicker/ProductSchema.js@@ -0,0 +1,31 @@+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.jsindex 6e01db53b2..9c13e9a369 100644--- a/saleor/static/js/storefront.js+++ b/saleor/static/js/storefront.js@@ -11,6 +11,7 @@ import passwordVisible from '../images/pass-visible.svg';import VariantPicker from './components/variantPicker/VariantPicker';import VariantPrice from './components/variantPicker/VariantPrice';+import ProductSchema from './components/variantPicker/ProductSchema';let csrftoken = $.cookie('csrftoken');@@ -115,7 +116,7 @@ $(function() {});});-// Input Passwords+// Input Passwordslet $inputPassword = $('input[type=password]');$("<img class='passIcon' src="+passwordIvisible+" />").insertAfter($inputPassword);@@ -258,6 +259,20 @@ if (variantPickerContainer) {}}+// Product Schema+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+ );+}++// Account delete address barlet $deleteAdressIcons = $('.icons');diff --git a/templates/product/details.html b/templates/product/details.htmlindex f432019181..d2c0549180 100644--- a/templates/product/details.html+++ b/templates/product/details.html@@ -29,7 +29,9 @@</div>{% endif %}<div class="row product">- <script type="application/ld+json">{{ json_ld_product_data|safe }}</script>+ <div id="product-schema-component">+ <script type="application/ld+json">{{ json_ld_product_data|safe }}</script>+ </div><div class="col-md-6 col-12 product__gallery">{% with images=product_images %}{% if images %}