Prevent clickable image in product page, disable product-image link
Asked Answered
A

3

6

With Woocommerce and my Storefront child theme, I am trying to prevent the fact that when clicking on a single-product image, it opens a new page with the full size image.

I want the image to be unclickable so nothing happens if the user clicks on the image in the product page.

In my child-theme functions.php, the following code prevents zooming and opening the image in a lightbox, but I want to fully deactivate the linking.

add_action( 'after_setup_theme', 'remove_hemen_theme_support', 100 );
function remove_hemen_theme_support() {
    remove_theme_support( 'wc-product-gallery-zoom' );
    remove_theme_support( 'wc-product-gallery-lightbox' );
}

How can I achieve this?

Anisaanise answered 1/10, 2020 at 17:3 Comment(0)
G
6

Add this filter, it should remove your hyperlink

function e12_remove_product_image_link( $html, $post_id ) {
    return preg_replace( "!<(a|/a).*?>!", '', $html );
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'e12_remove_product_image_link', 10, 2 );
Gentille answered 9/10, 2020 at 14:8 Comment(1)
Perfect - tested and working on WooCommerce 5.3.0. Thank you :)Bedclothes
I
1

you can overwrite the template woocommerce/single-product/product-thumbnails.php in your theme.

it runs the:

apply_filters(
    'woocommerce_single_product_image_thumbnail_html',
    sprintf(
      '<a href="%s" class="%s" title="%s" data-rel="prettyPhoto[product-gallery]">%s</a>',
      esc_url( $props['url'] ),
      esc_attr( $image_class ),
      esc_attr( $props['caption'] ),
      wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ), 0, $props )
    ),
    $attachment_id,
    $post->ID,
    esc_attr( $image_class )
);
Inflexed answered 14/10, 2020 at 20:20 Comment(0)
H
0

How I did it with the same theme.

I created a simple plugin (but you can use functions.php) with functions I need for override the theme and inside I have this code:

add_action( 'wp', 'ts_remove_zoom_lightbox_gallery_support', 99 );

function ts_remove_zoom_lightbox_gallery_support() {
     remove_theme_support( 'wc-product-gallery-zoom' ); 
     remove_theme_support( 'wc-product-gallery-lightbox' );
 }

The difference between our function is that I am using "wp" instead "after_setup_theme".

Try it and let me know if is work.

Housefly answered 9/10, 2020 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.