Autoplay a Product Video on hover on collection page

Let’s dive into how to autoplay a video on hover for a product card in a Shopify store. This feature can enhance the user experience on your website and make your product listings more engaging.

Autoplay a Product Video on hover on collection page
0 sections

How to do? Step by Step

- Step 1

Prepare a video for your product; it needs the same ratio as your current image. For example, If you are using a product image ratio of 5x7 (500x700px or 1000x1400px). You also need to create a video size of 5x7

- Step 2

Upload this video into Shopify Product Media

- Step 3

From Shopify Admin Dashboard > Online Store > Themes > ... button > Edit code

- Step #4

Search or Scroll the file list on the left to find the file name "card-product.liquid" within Snippets. (Depending on your store's theme, it will have different files. We will guide based on Dawn theme structure) .

- Step #5

Replace the code from line#85 to #104

by the new code:

{% assign has_video = 0 %}
{% for media in card_product.media %}
  {% if media.media_type == 'video' %}
    {% assign has_video = forloop.index0 %}
    {% break %}
  {% endif %}
{% endfor %}
{% if has_video == 0 %}
  {%- if card_product.media[1] != null and show_secondary_image -%}
    <img
      srcset="
        {%- if card_product.media[1].width >= 165 -%}{{ card_product.media[1] | image_url: width: 165 }} 165w,{%- endif -%}
        {%- if card_product.media[1].width >= 360 -%}{{ card_product.media[1] | image_url: width: 360 }} 360w,{%- endif -%}
        {%- if card_product.media[1].width >= 533 -%}{{ card_product.media[1] | image_url: width: 533 }} 533w,{%- endif -%}
        {%- if card_product.media[1].width >= 720 -%}{{ card_product.media[1] | image_url: width: 720 }} 720w,{%- endif -%}
        {%- if card_product.media[1].width >= 940 -%}{{ card_product.media[1] | image_url: width: 940 }} 940w,{%- endif -%}
        {%- if card_product.media[1].width >= 1066 -%}{{ card_product.media[1] | image_url: width: 1066 }} 1066w,{%- endif -%}
        {{ card_product.media[1] | image_url }} {{ card_product.media[1].width }}w
      "
      src="{{ card_product.media[1] | image_url: width: 533 }}"
      sizes="(min-width: {{ settings.page_width }}px) {{ settings.page_width | minus: 130 | divided_by: 4 }}px, (min-width: 990px) calc((100vw - 130px) / 4), (min-width: 750px) calc((100vw - 120px) / 3), calc((100vw - 35px) / 2)"
      alt=""
      class="motion-reduce"
      loading="lazy"
      width="{{ card_product.media[1].width }}"
      height="{{ card_product.media[1].height }}"
    >
  {%- endif -%}
{% else %}
  {% if card_product.media[has_video].sources[0].url contains '.mp4' %}
    <video src="{{card_product.media[has_video].sources[0].url}}" loop muted playsinline autoplay></video>
  {% else %}
    <video src="{{card_product.media[has_video].sources[1].url}}" loop muted playsinline autoplay></video>
  {% endif %}
{%- endif -%}

- Step #6

Save file

- Step #7

Open the Assets/component-card.css file or any CSS file to work with the Product Card. Add new css lines

.card-wrapper video{
  display:none !important;
}

.card-wrapper:hover video{
  display:block !important;
}
  • Save CSS file and complete the customize
  • Explanation

    {% assign has_video = 0 %}
    {% for media in card_product.media %}
      {% if media.media_type == 'video' %}
        {% assign has_video = forloop.index0 %}
        {% break %}
      {% endif %}
    {% endfor %}

    - This code is used to detect whether Product Media has a video or not. If yes, show the video; if no, show hover as an image.

    - has_video will return the position of the video in the Media

    - sources[0] use for short video and sources[1] use for long video

    Tags

    Related Articles

    Boost Sales Today: The Simple Power of Free Shipping Thresholds for Shopify Stores
    Tips & Tricks

    Boost Sales Today: The Simple Power of Free Shipping Thresholds for Shopify Stores

    Discover how implementing a strategic free shipping threshold can dramatically increase your Shopify store's conversion rates and average order value. Learn quick, actionable steps for both merchants and developers to set up and optimize this powerful tactic within a single day.

    May 16, 20266 min
    Boost Conversions: How to Add a Countdown Timer to Your Shopify Store
    Tips & Tricks

    Boost Conversions: How to Add a Countdown Timer to Your Shopify Store

    Learn how to add an urgency-driving countdown timer to your Shopify product pages and sales promotions. This tutorial covers methods from theme editor blocks to custom Liquid code and when to choose a dedicated app, helping you drive conversions.

    May 14, 20268 min
    Convert a Shopify Section to a Block (Horizon Theme version)
    Tips & Tricks

    Convert a Shopify Section to a Block (Horizon Theme version)

    In Horizon, Shopify allows developers to convert standalone sections into reusable blocks, giving merchants even more control in the theme editor. Let’s walk through the process step by step.

    May 10, 20267 min
    How to Add Tailwind CSS to Shopify
    Tips & Tricks

    How to Add Tailwind CSS to Shopify

    Learn how to add Tailwind CSS to Shopify for faster, responsive design. Enhance your store’s look with clean, utility-first styling. Perfect for theme customization

    May 10, 202615 min