Skip to content

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

Display 6 random related products

Mira Szewczyk1b3be4287194
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 5d15738aad..a0bfb04c38 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -1,5 +1,6 @@
import datetime
import json
+from random import randint, shuffle
from django.conf import settings
from django.http import HttpResponsePermanentRedirect, JsonResponse
@@ -145,7 +146,10 @@ def products_from_collections(request, product_id):
products = Product.objects.prefetch_related(
'collections', 'images').filter(
collections__products__id=product_id).exclude(
- id=product_id).distinct()[:6]
- ctx = {'products': products}
+ id=product_id).distinct()
+ products_count = products.count()
+ index = randint(0, products_count - 6)
+
+ ctx = {'products': products[index:index + 6]}
return TemplateResponse(request, 'product/_product_collections.html', ctx)
>>>>>>> Partial view with related products to display on product details page