Skip to content

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

Google preview snippet, CSS and Template

Pacu2fe7cc36cf2a0
diff --git a/saleor/page/models.py b/saleor/page/models.py
index 392e809dfe..4e2ce42ab3 100644
--- a/saleor/page/models.py
+++ b/saleor/page/models.py
@@ -4,6 +4,7 @@ from django.db import models
from django.db.models import Q
from django.urls import reverse
from django.utils.translation import pgettext_lazy
+from django.core.validators import MaxLengthValidator
from ..core.utils import build_absolute_uri
@@ -23,6 +24,12 @@ class Page(models.Model):
created = models.DateTimeField(auto_now_add=True)
is_visible = models.BooleanField(default=False)
available_on = models.DateField(blank=True, null=True)
+ seo_title = models.CharField(
+ max_length=70, blank=True, null=True,
+ validators=[MaxLengthValidator(70)])
+ seo_description = models.CharField(
+ max_length=300, blank=True, null=True,
+ validators=[MaxLengthValidator(300)])
objects = PageQuerySet.as_manager()
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 1a290e85c6..2a70703ae5 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -27,6 +27,12 @@ class Category(MPTTModel):
name = models.CharField(max_length=128)
slug = models.SlugField(max_length=128)
description = models.TextField(blank=True)
+ seo_title = models.CharField(
+ max_length=70, blank=True, null=True,
+ validators=[MaxLengthValidator(70)])
+ seo_description = models.CharField(
+ max_length=300, blank=True, null=True,
+ validators=[MaxLengthValidator(300)])
parent = models.ForeignKey(
'self', null=True, blank=True, related_name='children',
on_delete=models.CASCADE)
@@ -95,6 +101,9 @@ class Product(models.Model):
ProductType, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=128)
description = models.TextField()
+ seo_title = models.CharField(
+ max_length=70, blank=True, null=True,
+ validators=[MaxLengthValidator(70)])
seo_description = models.CharField(
max_length=300, blank=True, null=True,
validators=[MaxLengthValidator(300)])
@@ -395,6 +404,12 @@ class Collection(models.Model):
Product, blank=True, related_name='collections')
background_image = VersatileImageField(
upload_to='collection-backgrounds', blank=True, null=True)
+ seo_title = models.CharField(
+ max_length=70, blank=True, null=True,
+ validators=[MaxLengthValidator(70)])
+ seo_description = models.CharField(
+ max_length=300, blank=True, null=True,
+ validators=[MaxLengthValidator(300)])
class Meta:
ordering = ['pk']
diff --git a/saleor/static/dashboard/js/components/google-preview.js b/saleor/static/dashboard/js/components/google-preview.js
new file mode 100644
index 0000000000..1bf75338f3
--- /dev/null
+++ b/saleor/static/dashboard/js/components/google-preview.js
@@ -0,0 +1,17 @@
+const $seoTitle = $('#id_seo_title');
+const $seoDescription = $('#id_seo_description');
+
+$seoTitle.on('input propertychange', function (e) {
+ const $target = $(e.currentTarget);
+ const $currentText = $target.val();
+ const $googlePreview = $('#google-preview-title');
+ $googlePreview.text($currentText);
+});
+
+$seoDescription.on('input propertychange', function (e) {
+ console.log('DUPA');
+ const $target = $(e.currentTarget);
+ const $currentText = $target.val();
+ const $googlePreview = $('#google-preview-description');
+ $googlePreview.text($currentText);
+});
diff --git a/saleor/static/dashboard/js/components/index.js b/saleor/static/dashboard/js/components/index.js
index c4eb0208ef..f4cbec7c50 100644
--- a/saleor/static/dashboard/js/components/index.js
+++ b/saleor/static/dashboard/js/components/index.js
@@ -11,5 +11,6 @@ import './modal';
import './search';
import './selects';
import './side-menu';
+import './google-preview';
import './tab';
import './vouchers';
diff --git a/saleor/static/dashboard/scss/components/_google-preview.scss b/saleor/static/dashboard/scss/components/_google-preview.scss
new file mode 100644
index 0000000000..7e3e055c94
--- /dev/null
+++ b/saleor/static/dashboard/scss/components/_google-preview.scss
@@ -0,0 +1,43 @@
+.google-preview {
+ font-family: arial, sans-serif;
+ text-align: left;
+
+ .container {
+ padding: 12px;
+ border: 2px solid #ccc;
+ margin: 12px;
+ background-color: #fff;
+ max-width: 600px;
+ .title {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ a {
+ color: rgb(26, 13, 171);
+ cursor: pointer;
+ font-size: 18px;
+ line-height: 21.6px;
+ text-decoration: none solid rgb(26, 13, 171);
+ zoom: 1;
+ &:hover {
+ text-decoration: underline;
+ }
+ word-wrap: break-word;
+ }
+ }
+ .description {
+ color: rgb(84, 84, 84);
+ font-size: 13px;
+ line-height: 18.2px;
+ word-wrap: break-word;
+ zoom: 1;
+ }
+ .url {
+ color: rgb(0, 102, 33);
+ font-size: 13px;
+ line-height: 16px;
+ white-space: nowrap;
+ height: 16px;
+ }
+
+ }
+}
diff --git a/saleor/static/dashboard/scss/dashboard.scss b/saleor/static/dashboard/scss/dashboard.scss
index 5404c2b464..0b660c4672 100644
--- a/saleor/static/dashboard/scss/dashboard.scss
+++ b/saleor/static/dashboard/scss/dashboard.scss
@@ -26,6 +26,7 @@
@import "components/tabs";
@import "components/timeline";
@import "components/zero-page";
+@import "components/google-preview";
@import "layouts/orders";
@import "layouts/search-results";
diff --git a/templates/dashboard/includes/_google_preview.html b/templates/dashboard/includes/_google_preview.html
new file mode 100644
index 0000000000..ea84fe0821
--- /dev/null
+++ b/templates/dashboard/includes/_google_preview.html
@@ -0,0 +1,18 @@
+{% load build_absolute_uri from urls %}
+{% load materializecss %}
+
+<div class='google-preview'>
+ <div class='container'>
+ <div class='title'>
+ <a id="google-preview-title" href='#'>Example title</a>
+ </div>
+ {% if object.pk %}
+ <div class='url'>{% build_absolute_uri request=request location=object.get_absolute_url %}</div>
+ {% else %}
+ <div class='url'>{% build_absolute_uri request=request location="/" %}</div>
+ {% endif %}
+ <div id="google-preview-description" class='description'>{{ object.seo_description|default:_("Here is an example of what a snippet looks like in Google's Search Engine Results Page. ") }}</div>
+ </div>
+</div>
+{{ form.seo_title|materializecss }}
+{{ form.seo_description|materializecss }}
diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.html
index 4d06751b42..fe7c36dcfe 100644
--- a/templates/dashboard/product/form.html
+++ b/templates/dashboard/product/form.html
@@ -72,9 +72,6 @@
<div class="row">
{{ product_form.name|materializecss }}
</div>
- <div class="row">
- {{ product_form.seo_description|materializecss }}
- </div>
{% for attribute_field in product_form.iter_attribute_fields %}
<div class="row attribute-select-or-create">
{{ attribute_field|materializecss:"input-field s12" }}
@@ -83,6 +80,9 @@
<div class="row">
{{ product_form.description|materializecss }}
</div>
+ <div class="row">
+ {% include "dashboard/includes/_google_preview.html" with object=product form=product_form %}
+ </div>
</div>
<div class="col s12 l4">
<div class="row">