Woocommerce HooksWordpress HooksWp Tricks

Woocommerce hook add html to single product thumbnail

Woocommerce hook add html to single product thumbnail
Views

Hello to all, welcome to therichpost.com. 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.

Jassa

Thank you

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

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

Positive SSL