It's quite simple.
Steps
- Download slider CSS/JS file and store theme in folder(css/js)
- Include CSS/JS from functions.php or footer.php
- Overridden Single Product Image from plugin woocommerce/single-product/product-image.php to your active theme woocommerce/single-product/product-image.php
- Add/update page code accordingly and include a slider code
product-image.php example:
<?php
/**
* Single Product Image
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-image.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerceTemplates
* @version 3.5.1
*/
defined( 'ABSPATH' ) || exit;
// Note: `wc_get_gallery_image_html` was added in WC 3.3.2 and did not exist prior. This check protects against theme overrides being used on older versions of WC.
if ( ! function_exists( 'wc_get_gallery_image_html' ) ) {
return;
}
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.0/flexslider.css">
<?php
global $product;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 );
$post_thumbnail_id = $product->get_image_id();
$wrapper_classes = apply_filters(
'woocommerce_single_product_image_gallery_classes',
array(
'woocommerce-product-gallery',
'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ),
'woocommerce-product-gallery--columns-' . absint( $columns ),
'images',
)
);
$attachment_ids = $product->get_gallery_image_ids();
if ( $attachment_ids && $product->get_image_id() ) { ?>
<div class="<?php echo esc_attr( implode( ' ', array_map( 'sanitize_html_class', $wrapper_classes ) ) ); ?>" data-columns="<?php echo esc_attr( $columns ); ?>" style="opacity: 0; transition: opacity .25s ease-in-out;">
<figure>
<section class="slider">
<div id="slider" class="flexslider">
<ul class="slides">
<?php
foreach ( $attachment_ids as $attachment_id ) {
$image_url = wp_get_attachment_url($attachment_id);
?>
<li data-thumb="<?php echo $image_url; ?>">
<img src="<?php echo $image_url; ?>" />
</li>
<?php } ?>
</ul>
</div>
</section>
</figure>
</div>
<?php } ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.0/jquery.flexslider.js"></script>
<script type="text/javascript">
jQuery(window).load(function(){
jQuery('.flexslider').flexslider({
animation: "slide",
controlNav: "thumbnails",
start: function(slider){
jQuery('body').removeClass('loading');
}
});
});
</script>
Note: Currently I have included CSS/JS file/CDN on the product-image.php page just for example, but it's not the best way, so you should always include css/js file from functions.php or footer.php
You can change/update the HTML/slider structure accordingly.
Method 2:
If you want product gallery images with different site with title and then you will manage images accordingly then try this:
function wc_get_prioduct_gallery_image_with_title_html( $attachment_id, $main_image = false ) {
$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
$thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$image_size = apply_filters( 'woocommerce_gallery_image_size', $flexslider || $main_image ? 'woocommerce_single' : $thumbnail_size );
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$thumbnail_src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size );
$full_src = wp_get_attachment_image_src( $attachment_id, $full_size );
$image = wp_get_attachment_image( $attachment_id, $image_size, false, array(
'title' => get_post_field( 'post_title', $attachment_id ),
'data-caption' => get_post_field( 'post_excerpt', $attachment_id ),
'data-src' => $full_src[0],
'data-large_image' => $full_src[0],
'data-large_image_width' => $full_src[1],
'data-large_image_height' => $full_src[2],
'class' => $main_image ? 'wp-post-image' : '',
) );
$imageTitle = '<span>' . esc_html( get_the_title($attachment_id) ) . '</span>';
return '<div data-thumb="' . esc_url( $thumbnail_src[0] ) . '" class="woocommerce-product-gallery__image"><a href="' . esc_url( $full_src[0] ) . '">' . $image . $imageTitle . '</a></div>';
}
$attachment_ids = $product->get_gallery_image_ids();
$image = array();
foreach ($attachment_ids as $attachment_id) {
$image[] = wc_get_gallery_image_with_title_html($attachment_id);
}
echo "<pre>"; print_r($image); exit;
You can update function wc_get_prioduct_gallery_image_with_title_html() return html syntex accordingly.
If you will pass $attachment_id, you will get image as you set in return html