Randomize Products on Product Page

In order to display random products in your store you will have to use some custom Twig code:

{% set category_ = categories|last %}
{% set randomProducts = category_.randomProducts(3, [product.id]) %}
{% for randomProduct in randomProducts %}
  {% set is_on_sale = randomProduct.onSale %}
  {% set page_url = '/product/' ~ randomProduct.url_name %}
  • {{ randomProduct.images.first.description }}
    {% if is_on_sale %}SALE{% endif %}
  • {% else %} {% if empty_text %}
  • {{ empty_text }}
  • {% endif %} {% endfor %}

    This code snippet is picking the last category associated with the product from the product view, and rendering a list of random products.

    This code will only work when you are in a product view.