Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add product json-ld
Krzysztof Wolski18d6e880fbea
diff --git a/saleor/product/utils.py b/saleor/product/utils.pyindex cf128249fe..56b0718349 100644--- a/saleor/product/utils.py+++ b/saleor/product/utils.py@@ -1,6 +1,8 @@from collections import namedtuple+from django.conf import settingsfrom django.contrib.auth.models import AnonymousUser+from django.utils.encoding import smart_textfrom django_prices.templatetags import prices_i18nfrom ..cart.utils import get_cart_from_request, get_or_create_cart_from_request@@ -180,3 +182,36 @@ 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/product/views.py b/saleor/product/views.pyindex a6c56340cc..42f4372046 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -14,7 +14,7 @@ from .models import Categoryfrom .utils import (products_with_details, products_for_cart,handle_cart_form, get_availability,get_product_images, get_variant_picker_data,- get_product_attributes_data)+ get_product_attributes_data, product_json_ld)def product_details(request, slug, product_id, form=None):@@ -66,6 +66,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)return TemplateResponse(request, templates,{'is_visible': is_visible,@@ -76,7 +77,9 @@ def product_details(request, slug, product_id, form=None):'product_images': product_images,'show_variant_picker': show_variant_picker,'variant_picker_data': json.dumps(- variant_picker_data, default=serialize_decimal)})+ variant_picker_data, default=serialize_decimal),+ 'json_ld_product_data': json.dumps(+ json_ld_data, default=serialize_decimal)})def product_add_to_cart(request, slug, product_id):diff --git a/templates/product/details.html b/templates/product/details.htmlindex 6f7224d850..f432019181 100644--- a/templates/product/details.html+++ b/templates/product/details.html@@ -11,129 +11,128 @@{% block title %}{{ product }} — {{ block.super }}{% endblock %}{% block breadcrumb %}- <ul class="breadcrumbs list-unstyled">- <li><a href="/">{% trans "Home" %}</a></li>- {% with product.get_first_category as category %}- {% if category %}- <li><a href="{{ category.get_absolute_url }}">{{ category }}</a></li>- {% endif %}- {% endwith %}- <li><a href="{{ product.get_absolute_url }}">{{ product }}</a></li>- </ul>+ <ul class="breadcrumbs list-unstyled">+ <li><a href="/">{% trans "Home" %}</a></li>+ {% with product.get_first_category as category %}+ {% if category %}+ <li><a href="{{ category.get_absolute_url }}">{{ category }}</a></li>+ {% endif %}+ {% endwith %}+ <li><a href="{{ product.get_absolute_url }}">{{ product }}</a></li>+ </ul>{% endblock breadcrumb %}{% block content %}- {% if not is_visible %}- <div class="alert alert-warning" role="alert">- {% blocktrans with date=product.available_on|date %}<strong>Warning!</strong> You are previewing a product that will become visible on <strong>{{ date }}</strong>.{% endblocktrans %}- </div>- {% endif %}- <div itemscope itemtype="http://schema.org/Product">- <div class="row product">- <div class="col-md-6 col-12 product__gallery">- {% with images=product_images %}- {% if images %}- <div id="carousel-example-generic" class="carousel slide">- <ol class="carousel-indicators hidden-sm-down">- {% for image in images %}- {% if images|length > 1 %}- <li data-target="#carousel-example-generic" data-slide-to="{{ forloop.counter0 }}">- <img src="{% get_thumbnail image.image method="crop" size="60x60" %}" alt="">- </li>- {% endif %}- {% endfor %}- </ol>- <div class="carousel-inner" role="listbox">- {% for image in images %}- <div class="carousel-item{% if forloop.first %} active{% endif %}">- <img class="d-block img-fluid" src="{% get_thumbnail image.image method="crop" size="750x750" %}" alt="">- </div>- {% endfor %}- </div>- {% if images|length > 1 %}- <a class="carousel-control-prev" href="#carousel-example-generic" role="button" data-slide="prev">- <img class="icon-next" src="{% static "images/gallery-arrow.svg" %}">- </a>- <a class="carousel-control-next" href="#carousel-example-generic" role="button" data-slide="next">- <img class="icon-next" src="{% static "images/gallery-arrow.svg" %}">- </a>- {% endif %}- </div>- {% else %}- <img src="{% static 'images/product-image-placeholder.png' %}" alt="" class="img-fluid">- {% endif %}- {% endwith %}- </div>- <div class="col-md-6 col-12 product__info">- <h1 itemprop="name" class="product__info__name">- {{ product }}- </h1>- {% if user.is_staff %}- <p><a href="{% url "dashboard:product-update" pk=product.pk %}">{% trans "Edit in dashboard" %}</a></p>- {% endif %}- {% if availability.available %}- {% if show_variant_picker %}- <div id="variant-price-component"></div>- {% else %}- <h2 itemprop="offers" class="product__info__price" itemscope itemtype="http://schema.org/Offer">- <span>{% price_range availability.price_range %}</span>- {% if availability.discount %}- <small class="product__info__price__undiscounted">{% gross availability.price_range_undiscounted.min_price html=True %}</small>- {% endif %}- {% if availability.price_range_local_currency %}- <br>- <small class="text-info">- ≈- {% gross availability.price_range_local_currency.min_price html=True %}- </small>- {% endif %}- </h2>- {% endif %}- {% endif %}- {% if is_visible and product.is_in_stock %}- {% block orderform %}- {% if show_variant_picker %}- {% csrf_token %}- <div id="variant-picker" data-variant-picker-data="{{ variant_picker_data }}" data-action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}"></div>- {% else %}- <form id="product-form" role="form" class="product-form form-vertical" method="post"- action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" novalidate>- {% csrf_token %}- {% bootstrap_field form.variant %}- <div class="product__info__quantity">- {% bootstrap_field form.quantity %}- </div>-- <div class="form-group product__info__button">- <button class="btn primary">- {% trans "Add to cart" %}- </button>- </div>- </form>- {% endif %}- {% endblock %}- <div class="product__info__form-error">- <small class="text-danger"></small>- </div>-- {% else %}- <p class="alert alert-warning">{% trans "This product is currently <strong>unavailable</strong>." %}</p>+ {% if not is_visible %}+ <div class="alert alert-warning" role="alert">+ {% blocktrans with date=product.available_on|date %}<strong>Warning!</strong> You are previewing a product that will become visible on <strong>{{ date }}</strong>.{% endblocktrans %}+ </div>+ {% endif %}+ <div class="row product">+ <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 %}+ <div id="carousel-example-generic" class="carousel slide">+ <ol class="carousel-indicators hidden-sm-down">+ {% for image in images %}+ {% if images|length > 1 %}+ <li data-target="#carousel-example-generic" data-slide-to="{{ forloop.counter0 }}">+ <img src="{% get_thumbnail image.image method="crop" size="60x60" %}" alt="">+ </li>{% endif %}- <div class="product__info__description" itemprop="description">- <h3>{% trans "Description" %}</h3>- <hr>- {{ product.description|markdown }}+ {% endfor %}+ </ol>+ <div class="carousel-inner" role="listbox">+ {% for image in images %}+ <div class="carousel-item{% if forloop.first %} active{% endif %}">+ <img class="d-block img-fluid" src="{% get_thumbnail image.image method="crop" size="750x750" %}" alt=""></div>- <hr>- <table>- {% for attribute, value in product_attributes.items %}- <tr>- <td>{{ attribute }}:</td>- <td><strong>{{ value }}</strong></td>- </tr>- {% endfor %}- </table>+ {% endfor %}</div>+ {% if images|length > 1 %}+ <a class="carousel-control-prev" href="#carousel-example-generic" role="button" data-slide="prev">+ <img class="icon-next" src="{% static "images/gallery-arrow.svg" %}">+ </a>+ <a class="carousel-control-next" href="#carousel-example-generic" role="button" data-slide="next">+ <img class="icon-next" src="{% static "images/gallery-arrow.svg" %}">+ </a>+ {% endif %}+ </div>+ {% else %}+ <img src="{% static 'images/product-image-placeholder.png' %}" alt="" class="img-fluid">+ {% endif %}+ {% endwith %}+ </div>+ <div class="col-md-6 col-12 product__info">+ <h1 class="product__info__name">+ {{ product }}+ </h1>+ {% if user.is_staff %}+ <p><a href="{% url "dashboard:product-update" pk=product.pk %}">{% trans "Edit in dashboard" %}</a></p>+ {% endif %}+ {% if availability.available %}+ {% if show_variant_picker %}+ <div id="variant-price-component"></div>+ {% else %}+ <h2 class="product__info__price">+ <span>{% price_range availability.price_range %}</span>+ {% if availability.discount %}+ <small class="product__info__price__undiscounted">{% gross availability.price_range_undiscounted.min_price html=True %}</small>+ {% endif %}+ {% if availability.price_range_local_currency %}+ <br>+ <small class="text-info">+ ≈+ {% gross availability.price_range_local_currency.min_price html=True %}+ </small>+ {% endif %}+ </h2>+ {% endif %}+ {% endif %}+ {% if is_visible and product.is_in_stock %}+ {% block orderform %}+ {% if show_variant_picker %}+ {% csrf_token %}+ <div id="variant-picker" data-variant-picker-data="{{ variant_picker_data }}" data-action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}"></div>+ {% else %}+ <form id="product-form" role="form" class="product-form form-vertical" method="post"+ action="{% url 'product:add-to-cart' product_id=product.pk slug=product.get_slug %}" novalidate>+ {% csrf_token %}+ {% bootstrap_field form.variant %}+ <div class="product__info__quantity">+ {% bootstrap_field form.quantity %}+ </div>++ <div class="form-group product__info__button">+ <button class="btn primary">+ {% trans "Add to cart" %}+ </button>+ </div>+ </form>+ {% endif %}+ {% endblock %}+ <div class="product__info__form-error">+ <small class="text-danger"></small></div>++ {% else %}+ <p class="alert alert-warning">{% trans "This product is currently <strong>unavailable</strong>." %}</p>+ {% endif %}+ <div class="product__info__description">+ <h3>{% trans "Description" %}</h3>+ <hr>+ {{ product.description|markdown }}+ </div>+ <hr>+ <table>+ {% for attribute, value in product_attributes.items %}+ <tr>+ <td>{{ attribute }}:</td>+ <td><strong>{{ value }}</strong></td>+ </tr>+ {% endfor %}+ </table></div>+ </div>{% endblock content %}