Categories

Saturday, December 21, 2024
#919814419350 therichposts@gmail.com
Woocommerce HooksWordpress HooksWp Tricks

Woocommerce hook add html to single product thumbnail

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, 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.