skip to Main Content

I require some help in creating a product slider in Shopify that allows you to add images and videos. At the moment the slider is working fine but I am having issues adding product videos to the slider.

From what I have read you can use the alt tag to add a YouTube embed code that will overwrite the image and display the video. I can get this working for the first image but it has a tenancy to break the rest of the slider. I believe I might need to change up the default slider and head with another alternative.

Here is what I have so far

  $('#FeaturedImageZoom-product-template .product-featured-img').each (function() {

    var videoid = $(this).attr("alt");
    console.log(videoid);

    if ($('.product-featured-img').attr("alt") == videoid) {         
      $(this).replaceWith('<iframe width="500px" height="500px" src="'+videoid+'?autoplay=1&amp;showinfo=0&amp;controls=0&amp;modestbranding=1&amp;rel=0" frameborder="0" allowfullscreen></iframe>');
    } 

  });

And here is the liquid code to go with that:

<div class="grid product-single">
<div class="grid__item product-single__photos {{ product_image_width }}{% if section.settings.image_size == 'full' %} product-single__photos--full{% endif %}">
  {%- assign featured_img_src = current_variant.featured_image.src | default: product.featured_image.src -%}
  {%- assign featured_img_alt = current_variant.featured_image.alt | default: product.featured_image.alt -%}
  <div id="FeaturedImageZoom-{{ section.id }}" class="product-single__photo{% if product.images.size > 1 %} product-single__photo--has-thumbnails{% endif %}" {% if enable_zoom %}data-zoom="{{ featured_img_src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
    <img src="{{ featured_img_src | img_url: product_image_size, scale: product_image_scale }}" alt="{{ featured_img_alt }}" id="FeaturedImage-{{ section.id }}" class="product-featured-img{% if enable_zoom %} js-zoom-enabled{% endif %}">
  </div>

  {% if product.images.size > 1 %}
    {% if product.images.size > 3 %}
      {%- assign enable_thumbnail_slides = true -%}
    {% endif %}

    <div class="thumbnails-wrapper{% if enable_thumbnail_slides == true %} thumbnails-slider--active{% endif %}">
      {% if enable_thumbnail_slides == true %}
        <button type="button" class="btn btn--link medium-up--hide thumbnails-slider__btn thumbnails-slider__prev thumbnails-slider__prev--{{ section.id }}">
          {% include 'icon-chevron-left' %}
          <span class="icon__fallback-text">{{ 'sections.slideshow.previous_slide' | t }}</span>
        </button>
      {% endif %}
      <ul class="grid grid--uniform product-single__thumbnails product-single__thumbnails-{{ section.id }}">
        {% for image in product.images %}
          <li class="grid__item {{ product_thumbnail_width }} product-single__thumbnails-item">
            <a
              href="{{ image.src | img_url: product_image_size, scale: product_image_scale }}"
                class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}"
                {% if enable_zoom %}data-zoom="{{ image.src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
                <img class="product-single__thumbnail-image" src="{{ image.src | img_url: product_thumb_size, scale: product_image_scale }}" alt="{{ image.alt | escape }}">
            </a>
          </li>
        {% endfor %}
      </ul>
      {% if enable_thumbnail_slides == true %}
        <button type="button" class="btn btn--link medium-up--hide thumbnails-slider__btn thumbnails-slider__next thumbnails-slider__next--{{ section.id }}">
          {% include 'icon-chevron-right' %}
          <span class="icon__fallback-text">{{ 'sections.slideshow.next_slide' | t }}</span>
        </button>
      {% endif %}
    </div>
  {% endif %}
</div>

I have read a couple of posts that outline a similar issue with developers trying to add a product videos however I have yet to come across a solution.

If anyone can help out here that would be appreciated.

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    I just wanted to post the solution I arrived at based on the answer above by "drip". I hope this might help someone else down the track.

      <!-- Product Slider w/ Video -->
      <div id="product-flexslider" class="flexslider product_gallery product-{{ product.id }}-gallery {% if product-images == blank %}product_slider{% endif %} {% if settings.product_thumbs == false %}animated fadeInUp{% endif %}">
        <ul class="slides">
          {% for image in product.images %}
          <li data-thumb="{{ image | product_img_url: '1024x1024' }}" data-title="{% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}{{ product.title }}{% else %}{{ image.alt | escape }}{% endif %}">
            {% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}
            {% assign src = image.alt | split: 'src="' %}
            {% assign src = src[1] | split: '"' | first %}
    
            {% if src contains '?' %}
            {% assign src = src | append: '&amp;autoplay=1' %}
            {% else %}
            {% assign src = src | append: '?autoplay=1' %}
            {% endif %}
    
            <div class="video-container {% if image.alt contains 'vimeo' %}vimeo{% else %}youtube{% endif %}">
              <div>
                <a href="{{ src }}" title="{{ product.title | escape }}">
                  {{ image.alt }}
                </a>
              </div>
            </div>
            {% else %}
            <a href="{{ image | product_img_url: 'master' }}" title="{{ image.alt | escape }}">
              <img  src="{{ image | product_img_url: '1024x1024' }}" alt="{{ image.alt | escape }}"/>
            </a>
            {% endif %}
          </li>
          {% endfor %}
        </ul>
      </div>
    
      <!-- Carousel -->
    
      <div id="product-carousel" class="flexslider product_gallery product-{{ product.id }}-gallery {% if product-images == blank %}product_slider{% endif %} {% if settings.product_thumbs == false %}animated fadeInUp{% endif %}">
        <ul class="slides">
          {% for image in product.images %}
          <li data-thumb="{{ image | product_img_url: '1024x1024' }}" data-title="{% if image.alt contains 'youtube' or image.alt contains 'vimeo' %}{{ product.title }}{% else %}{{ image.alt | escape }}{% endif %}">
    
            <a href="{{ image | product_img_url: 'master' }}" title="{{ image.alt | escape }}">
              <img src="{{ image | product_img_url: '1024x1024' }}" alt="{{ image.alt | escape }}" />
            </a>
          </li>
          {% endfor %}
        </ul>
      </div>
      <!-- End Slider -->
    
    <!-- FlexSlider -->
    
    {{ 'jquery.flexslider-min.js' | asset_url | script_tag }}
    {{ 'flexslider.css' | asset_url | stylesheet_tag }}
    
    
    <script type="text/javascript" charset="utf-8">
    (function($) {
      $(window).load(function(){
       $('#product-carousel').flexslider({
        animation: "slide",
        controlNav: false,
        itemWidth: 41,
        itemMargin: 5,
        asNavFor: '#product-flexslider'
      });
      $('#product-flexslider').flexslider({
        animation: "slide",
        controlNav: false,
        sync: "#product-carousel"
      });
    });
    })(jQuery);
    
    </script>
    

    Once you have updated the code you can place the embed code in the ALT tag of the image while using the image as a thumbnail placeholder.

    FlexSlider properties can be located here: https://github.com/woocommerce/FlexSlider/wiki/FlexSlider-Properties

    The solution was also helped by the post here which was relating to another issue: https://ecommerce.shopify.com/c/ecommerce-discussion/t/issue-with-variable-image-444159


  2. Why use javascript for this when you can use simply liquid?

    You just need to add check for the alt attribute if it contains youtube or something unique.

    So for example:

    {% if image.alt contains 'youtube' %}
        <iframe width="500px" height="500px" src="{{image.alt}}?autoplay=1&amp;showinfo=0&amp;controls=0&amp;modestbranding=1&amp;rel=0" frameborder="0" allowfullscreen></iframe>
    {% else %}
        <a
          href="{{ image.src | img_url: product_image_size, scale: product_image_scale }}"
            class="text-link product-single__thumbnail product-single__thumbnail--{{ section.id }}"
            {% if enable_zoom %}data-zoom="{{ image.src | img_url: product_image_zoom_size, scale: product_image_scale }}"{% endif %}>
            <img class="product-single__thumbnail-image" src="{{ image.src | img_url: product_thumb_size, scale: product_image_scale }}" alt="{{ image.alt | escape }}">
        </a>
    {% endif %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search