Wednesday, July 24, 2024
Woocommerce HooksWordpress HooksWp Tricks

Woocommerce hook add html to single product thumbnail

Hello to all, welcome to In this post, I will tell you, Woocommerce hook add html to single product thumbnail.

With the help of this code, you will add custom html to product thumbnail image on single product page.

Here is the working code and please use this carefully into your theme’s functions.pgp file:

add_filter( 'woocommerce_single_product_image_thumbnail_html', 'add_class_to_thumbs', 10, 2 );
function add_class_to_thumbs( $html, $attachment_id ) {
    global $product;
   global $woocommerce;
   $src  = wp_get_attachment_image_src( get_post_thumbnail_id( $product->ID ), 'full' );
   if ( get_post_thumbnail_id() !== intval( $attachment_id ) ) {
       $html  = '<div class="woocommerce-product-gallery__image--placeholder singleProductImg">';

       // I am wraping then product thumbnail into my custom div class-name 'productImg'

       $html .= sprintf( '<div class="productImg"><img src="'.$src[0].'" alt="%s" class="wp-post-image" /></div>', esc_url( wc_placeholder_img_src( 'woocommerce_single' ) ), esc_html__( 'Awaiting product image', 'woocommerce' ) );
       $html .= '</div>';

   return $html;


This is it. If you have any query related to this post then comment below.


Thank you

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.