简体   繁体   English

WooCommerce 中产品缩略图的滑块

[英]Slider for product thumbnails in WooCommerce

I've built an own theme for my WooCommerce store.我为我的 WooCommerce 商店建立了自己的主题。 At the moment I've made very few changes to the existing template files.目前我对现有的模板文件做了很少的改动。

But I want to change the view of the image slider on the product page.但我想更改产品页面上图像滑块的视图。 At the moment, I'm using the basic output with some basic styling.目前,我正在使用带有一些基本样式的基本输出。

The problem is, that the thumbnails are shown in multiple rows if there are not fitting in a single row.问题是,如果单行中没有合适的缩略图,则缩略图会显示在多行中。

What I need, is a slider for the thumbnails.我需要的是缩略图滑块。 I tried to change the template file product-thumbnails.php .我试图更改模板文件product-thumbnails.php But it uses a function called wc_get_gallery_image_html .但它使用了一个名为wc_get_gallery_image_html的函数。 I don't know how to change that in a good way.我不知道如何以一种好的方式改变它。

Then I realized that WooCommerce uses the FlexSlider plugin.然后我意识到 WooCommerce 使用了 FlexSlider 插件。 And this plugin could provide waht I want/need.这个插件可以提供我想要/需要的东西。

See here:看这里:

http://flexslider.woothemes.com/thumbnail-slider.html http://flexslider.woothemes.com/thumbnail-slider.html

I just don't know, how I could to add that behavior to my product image slider.我只是不知道如何将这种行为添加到我的产品图片滑块中。

I saw that there is an example JS and HTML code in the example from above.我看到上面的示例中有一个示例 JS 和 HTML 代码。 But I don't know how I could adapt it to my slider because my HTML output looks a lot different and renaming the IDs doesn't work.但我不知道如何将它调整到我的滑块,因为我的 HTML 输出看起来有很大不同,重命名 ID 不起作用。

EDIT:编辑:

I found a way to add options to the slider.我找到了一种向滑块添加选项的方法。 The answer is from here:答案来自这里:

https://stackoverflow.com/a/46448407/1788961 https://stackoverflow.com/a/46448407/1788961

I added the following code (and it's working) to add options to my slider:我添加了以下代码(并且它正在运行)为我的滑块添加选项:

// Update WooCommerce Flexslider options
add_filter('woocommerce_single_product_carousel_options', 'ud_update_woo_flexslider_options');

function ud_update_woo_flexslider_options($options)
{

  $options['directionNav'] = true;
  $options['sync'] = '.flex-control-thumbs';

  return $options;
}

The problem is, that I need to add options to the thumbnails as well.问题是,我还需要为缩略图添加选项。 Is there any way to do that?有没有办法做到这一点?

At the moment the HTML output looks like this:目前 HTML 输出如下所示:

<div
  class="woocommerce-product-gallery woocommerce-product-gallery--with-images woocommerce-product-gallery--columns-4 images"
  data-columns="4" style="opacity: 1; transition: opacity 0.25s ease-in-out;">
  <a href="[path]" class="woocommerce-product-gallery__trigger">🔍</a>
  <div class="flex-viewport" style="overflow: hidden; position: relative; height: 400px;">
    <figure class="woocommerce-product-gallery__wrapper"
      style="width: 2600%; transition-duration: 0s; transform: translate3d(-1911px, 0px, 0px);">
      <div data-thumb="[path]" class="woocommerce-product-gallery__image flex-active-slide" data-thumb-alt=""
        style="width: 637px; margin-right: 0px; float: left; display: block; position: relative; overflow: hidden;">
        <a href="[path]">
          <img width="600" height="400" src="[path]" class="" alt="" title="[title]" data-caption="" data-src="[path]"
            data-large_image="[path]" data-large_image_width="2000" data-large_image_height="1333" srcset="[path]"
            sizes="(max-width: 600px) 100vw, 600px" draggable="false">
        </a>
        <img src="[path]" class="zoomImg"
          style="position: absolute; top: 0px; left: 0px; opacity: 0; width: 2000px; height: 1333px; border: none; max-width: none; max-height: none;">
      </div>
      <div data-thumb="[path]" class="woocommerce-product-gallery__image" data-thumb-alt=""
        style="width: 637px; margin-right: 0px; float: left; display: block; position: relative; overflow: hidden;">
        <a href="[path]">
          <img width="600" height="400" src="[path]" class="" alt="" title="[title]" data-caption="" data-src="[path]"
            data-large_image="[path]" data-large_image_width="2000" data-large_image_height="1333" srcset="[path]"
            sizes="(max-width: 600px) 100vw, 600px" draggable="false">
        </a>
        <img src="[path]" class="zoomImg"
          style="position: absolute; top: -578.46px; left: -7.48901px; opacity: 0; width: 2000px; height: 1333px; border: none; max-width: none; max-height: none;">
      </div>
      <!-- ...more items... -->
    </figure>
  </div>

  <ol class="flex-control-nav flex-control-thumbs">
    <li><img src="[path]" draggable="false" class="flex-active"></li>
    <li><img src="[path]" draggable="false" class=""></li>
    <!-- ...more items... -->
  </ol>
</div>

EDIT: See above, the part below is not working.编辑:见上文,下面的部分不起作用。 This is what I've tried:这是我尝试过的:

$('.flex-control-thumbs').flexslider({
  animation: "slide",
  controlNav: false,
  animationLoop: false,
  slideshow: false,
  itemWidth: 210,
  itemMargin: 5,
  asNavFor: '.woocommerce-product-gallery__wrapper'
});

$('.woocommerce-product-gallery__wrapper').flexslider({
  animation: "slide",
  controlNav: false,
  animationLoop: false,
  slideshow: false,
  sync: ".flex-control-thumbs"
});

Theres a fle in the woocommerce directory which has all the settings of Product Thumbnail slider where you can edit the settings. woocommerce 目录中有一个文件,其中包含产品缩略图滑块的所有设置,您可以在其中编辑设置。

wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php

$params = array(
    'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
    'review_rating_required'    => get_option( 'woocommerce_review_rating_required' ),
    'flexslider'                => apply_filters(
        'woocommerce_single_product_carousel_options', array(
            'rtl'            => is_rtl(),
            'animation'      => 'slide',
            'smoothHeight'   => true,
            'directionNav'   => false,
            'controlNav'     => 'thumbnails',
            'slideshow'      => false,
            'animationSpeed' => 500,
            'animationLoop'  => true, // Breaks photoswipe pagination if true.
            'allowOneSlide'  => false,
        )
    ),
);

You can change the flexslider attributes by using the filter, like this:您可以使用过滤器更改 flexslider 属性,如下所示:

add_filter( 'woocommerce_single_product_carousel_options', 'customslug_single_product_carousel_options', 99, 1 );
function customslug_single_product_carousel_options( $options ) {
    $options['animation'] = 'fade';
    $options['animationSpeed'] = 400;
    return $options;
}

Try this Plugin : https://wordpress.org/plugins/advanced-woocommerce-product-gallery-slider/ 试试这个插件: https : //wordpress.org/plugins/advanced-woocommerce-product-gallery-slider/

  • Gallery Layout [Horizontal/Vertical Silder] 画廊布局[水平/垂直贴图]
  • Responsive Layout 响应式布局
  • Very Lightweight 轻巧
  • Easy Admin Settings 简易管理员设置
  • Navigation support 导航支持
  • Slider AutoPlay Options 滑块自动播放选项
  • Customize Slider Arrow (Color Options) 自定义滑块箭头(颜色选项)
  • Attractive Lightbox effect 迷人的灯箱效果
  • Working with Most of Premium themes 处理大多数高级主题
  • Touch and Swipe enabled etc. 启用触摸和滑动等。

Best way forward is to create your own slider instead of using woocomerce built in one.最好的方法是创建自己的滑块,而不是使用内置的 woocomerce。 Tried different ways of customisation but in vain.尝试了不同的定制方式,但徒劳无功。 I customised template files and added my own structure in product-thumbnails.php我自定义了模板文件并在 product-thumbnails.php 中添加了我自己的结构

<div class="pd-gallery-slider">
    <div class="swiper-container">
       <div class="swiper-wrapper">
        <?php
            foreach($attachment_ids as $attachment_id) {
                $image_url = wp_get_attachment_url($attachment_id);
                ?>
                <div class="swiper-slide pd-gal-slide fx fx-jc fx-ac">
                    <a class="fx fx-wrap fx-ae" data-fancybox="gallery" href="<?php echo $image_url; ?>" style="background: url(<?php echo $image_url; ?>) center no-repeat;background-size:cover;">
                        <img src="<?php echo $image_url; ?>" alt="gallery">
                    </a>
                </div>
            <?php }
        ?>
    </div>
    <div class="gal-btn-prev fx"></div>
    <div class="gal-btn-next fx"></div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM