Skip to content

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

Simplify first image fetching

Artur Smęt349956f5f2ee
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.py
index e51c4cb9ed..bf6ac4dad5 100644
--- a/saleor/product/templatetags/product_images.py
+++ b/saleor/product/templatetags/product_images.py
@@ -22,7 +22,7 @@ AVAILABLE_SIZES = get_available_sizes()
@register.simple_tag()
-def product_image(instance, size, method='crop'):
+def get_thumbnail(instance, size, method='crop'):
if instance:
size_name = '%s__%s' % (method, size)
if (size_name not in AVAILABLE_SIZES and not
@@ -40,4 +40,14 @@ def product_image(instance, size, method='crop'):
extra={'instance': instance, 'size': size})
else:
return thumbnail.url
- return static('dist/images/product-image-placeholder.png')
+ return static('images/product-image-placeholder.png')
+
+
+def product_first_image(product, size, method='crop'):
+ """
+ Returns main product image
+ """
+ all_images = product.images.all()
+ main_image = all_images[0].image if all_images else None
+ return get_thumbnail(main_image, size, method)
diff --git a/templates/category/_items.html b/templates/category/_items.html
index 8f5d8b041c..dcf40c7061 100644
--- a/templates/category/_items.html
+++ b/templates/category/_items.html
@@ -2,20 +2,14 @@
{% load staticfiles %}
{% load price_range from price_ranges %}
{% load discounted_price_range price_difference from discount %}
-{% load product_image from product_images %}
+{% load product_first_image from product_images %}
{% for product in products %}
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3" itemscope itemtype="https://schema.org/Product">
<div class="panel panel-default text-center">
<div class="panel-body">
<a itemprop="url" href="{{ product.get_absolute_url }}">
- {% with product_image=product.get_first_image %}
- {% if product_image %}
- <img itemprop="image" class="img-responsive" src="{% product_image product_image method="crop" size="400x400" %}" alt="">
- {% else %}
- <img src="{% static 'images/product-image-placeholder.png' %}" alt="" class="img-responsive">
- {% endif %}
- {% endwith %}
+ <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>
</a>
</div>
diff --git a/templates/product/details.html b/templates/product/details.html
index b7b714e476..e7d5a697b5 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -5,7 +5,7 @@
{% load gross from prices_i18n %}
{% load markdown from markdown %}
{% load price_range from price_ranges %}
-{% load product_image from product_images %}
+{% load product_first_image get_thumbnail from product_images %}
{% load static from staticfiles %}
{% block title %}{{ product }} — {{ block.super }}{% endblock %}
@@ -56,11 +56,7 @@
<hr>
<div class="visible-xs">
- {% if product_images %}
- <img class="img-responsive" src="{% product_image product_images.0.image method="crop" size="680x680" %}" alt="">
- {% else %}
- <img src="{% static 'images/product-image-placeholder.png' %}" alt="" class="img-responsive">
- {% endif %}
+ <img class="img-responsive" src="{% product_first_image product method="crop" size="680x680" %}" alt="">
<hr>
</div>
@@ -97,7 +93,7 @@
{% for image in images %}
<div class="item{% if forloop.first %} active{% endif %}">
<a href="#" data-toggle="modal" data-target="#modal-{{ forloop.counter }}">
- <img class="img-responsive" src="{% product_image image.image method="crop" size="750x750" %}" alt="">
+ <img class="img-responsive" src="{% get_thumbnail image.image method="crop" size="750x750" %}" alt="">
</a>
</div>
{% empty %}
@@ -129,7 +125,7 @@
<div class="product-gallery row">
{% for image in images %}
<a href="{{ image.image.url }}" target="_blank" class="product-gallery-item col-xs-6 col-md-3">
- <img class="thumbnail img-responsive" src="{% product_image image.image method="crop" size="280x280" %}" alt="">
+ <img class="thumbnail img-responsive" src="{% get_thumbnail image.image method="crop" size="280x280" %}" alt="">
</a>
{% endfor %}
</div>