Commit 5b05a100380 for woocommerce
commit 5b05a100380f3474cd9221153e027cc855b6d0be
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date: Fri Jun 12 00:59:43 2026 +0530
Render product image without link when disabled (#65682)
* Render product image without link when disabled
- Return the image markup directly when `showProductLink` is false
- Add coverage for default linked output and hidden-link output
* Add changefile(s) from automation for the following project(s): woocommerce
* Add changefile(s) from automation for the following project(s): woocommerce
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/65682-64660-image-no-link-archive b/plugins/woocommerce/changelog/65682-64660-image-no-link-archive
new file mode 100644
index 00000000000..5b9d6f107d1
--- /dev/null
+++ b/plugins/woocommerce/changelog/65682-64660-image-no-link-archive
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Updates anchor rendering logic to not show an anchor tag when product image link is disabled
\ No newline at end of file
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductImage.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductImage.php
index 2fe6dbe246f..0263d38edca 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductImage.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductImage.php
@@ -103,21 +103,20 @@ class ProductImage extends AbstractBlock {
private function render_anchor( $product, $on_sale_badge, $product_image, $attributes, $inner_blocks_content ) {
$product_permalink = $product->get_permalink();
- $is_link = isset( $attributes['showProductLink'] ) ? $attributes['showProductLink'] : true;
- $href_attribute = $is_link ? sprintf( 'href="%s"', esc_url( $product_permalink ) ) : 'href="#" onclick="return false;"';
- $wrapper_style = ! $is_link ? 'pointer-events: none; cursor: default;' : '';
- $directive = $is_link ? 'data-wp-on--click="woocommerce/product-collection::actions.viewProduct"' : '';
+ $is_link = isset( $attributes['showProductLink'] ) ? $attributes['showProductLink'] : true;
$inner_blocks_container = sprintf(
'<div class="wc-block-components-product-image__inner-container">%s</div>',
$inner_blocks_content
);
+ if ( ! $is_link ) {
+ return $on_sale_badge . $product_image . $inner_blocks_container;
+ }
+
return sprintf(
- '<a %1$s style="%2$s" %3$s>%4$s%5$s%6$s</a>',
- $href_attribute,
- esc_attr( $wrapper_style ),
- $directive,
+ '<a href="%1$s" data-wp-on--click="woocommerce/product-collection::actions.viewProduct">%2$s%3$s%4$s</a>',
+ esc_url( $product_permalink ),
$on_sale_badge,
$product_image,
$inner_blocks_container
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductImage.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductImage.php
index 19be72a41bb..0c145a9c452 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductImage.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductImage.php
@@ -116,6 +116,41 @@ class ProductImage extends \WP_UnitTestCase {
wp_delete_attachment( $data['image_id'], true );
}
+ /**
+ * Test that the ProductImage block renders a link by default.
+ */
+ public function test_product_image_render_includes_product_link_by_default() {
+ $data = $this->create_product_with_image();
+
+ $markup = do_blocks( '<!-- wp:woocommerce/single-product {"productId":' . $data['product']->get_id() . '} --><!-- wp:woocommerce/product-image /--><!-- /wp:woocommerce/single-product -->' );
+
+ $this->assertStringContainsString( '<a href="' . $data['product']->get_permalink() . '"', $markup );
+ $this->assertStringContainsString( 'data-wp-on--click="woocommerce/product-collection::actions.viewProduct"', $markup );
+
+ // Clean up.
+ $data['product']->delete( true );
+ wp_delete_attachment( $data['image_id'], true );
+ }
+
+ /**
+ * Test that the ProductImage block does not render a disabled link when product links are hidden.
+ */
+ public function test_product_image_render_omits_anchor_when_product_link_is_hidden() {
+ $data = $this->create_product_with_image();
+
+ $markup = do_blocks( '<!-- wp:woocommerce/single-product {"productId":' . $data['product']->get_id() . '} --><!-- wp:woocommerce/product-image {"showProductLink":false} /--><!-- /wp:woocommerce/single-product -->' );
+
+ $this->assertStringContainsString( 'data-testid="product-image"', $markup );
+ $this->assertStringNotContainsString( '<a ', $markup );
+ $this->assertStringNotContainsString( 'href="#"', $markup );
+ $this->assertStringNotContainsString( 'onclick="return false;"', $markup );
+ $this->assertStringNotContainsString( 'data-wp-on--click="woocommerce/product-collection::actions.viewProduct"', $markup );
+
+ // Clean up.
+ $data['product']->delete( true );
+ wp_delete_attachment( $data['image_id'], true );
+ }
+
/**
* Test that the ProductImage block renders correctly for a variable product with variation images.
* This is the main test case: if product is variable product and has some images attached to the variation