Commit 5cc04dc9f42 for woocommerce
commit 5cc04dc9f42c2b7ba719db2511fcb43f3985910d
Author: Bero <1199991+bgrgicak@users.noreply.github.com>
Date: Mon Nov 2 13:53:36 2026 +0100
Fix missing images in product link previews (#70399)
* Fix missing images in product link previews
* Add changelog entry for product link previews
* Simplify product image selection
* Add regression coverage for product preview images
* Add deleted gallery attachment regression coverage
diff --git a/plugins/woocommerce/changelog/fix-open-graph-gallery-images b/plugins/woocommerce/changelog/fix-open-graph-gallery-images
new file mode 100644
index 00000000000..a47050f16be
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-open-graph-gallery-images
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Include available product images in shared link previews when gallery images are missing.
diff --git a/plugins/woocommerce/src/Internal/StructuredData/ProductOpenGraphTags.php b/plugins/woocommerce/src/Internal/StructuredData/ProductOpenGraphTags.php
index 21fab5dfefb..e4d4cfbe2ec 100644
--- a/plugins/woocommerce/src/Internal/StructuredData/ProductOpenGraphTags.php
+++ b/plugins/woocommerce/src/Internal/StructuredData/ProductOpenGraphTags.php
@@ -47,7 +47,7 @@ class ProductOpenGraphTags {
}
$gallery = $product->get_gallery_image_ids();
$image_ids = array_merge( array( $product->get_image_id() ), is_array( $gallery ) ? $gallery : array() );
- $image_ids = array_slice( array_unique( array_filter( array_map( 'intval', $image_ids ) ) ), 0, 6 );
+ $image_ids = array_unique( array_filter( array_map( 'intval', $image_ids ) ) );
if ( ! empty( $image_ids ) ) {
// Prime caches to reduce future queries.
_prime_post_caches( $image_ids );
@@ -58,6 +58,9 @@ class ProductOpenGraphTags {
if ( $image && ! in_array( $image['og:image'], $urls, true ) ) {
$tags[] = $image;
$urls[] = $image['og:image'];
+ if ( 6 === count( $urls ) ) {
+ break;
+ }
}
}
$tags[] = array( 'twitter:card' => $urls ? 'summary_large_image' : 'summary' );
diff --git a/plugins/woocommerce/tests/php/src/Internal/StructuredData/OpenGraphTest.php b/plugins/woocommerce/tests/php/src/Internal/StructuredData/OpenGraphTest.php
index b23a47a582a..5ad84a54cb6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/StructuredData/OpenGraphTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/StructuredData/OpenGraphTest.php
@@ -114,6 +114,51 @@ class OpenGraphTest extends WC_Unit_Test_Case {
$this->assertSame( array( 'USD' ), $tags['product:price:currency'], 'The store currency should be added.' );
}
+ /**
+ * @testdox Should collect up to six distinct valid product image URLs.
+ */
+ public function test_collects_six_distinct_valid_images(): void {
+ $deleted_id = self::factory()->attachment->create(
+ array(
+ 'file' => 'deleted.jpg',
+ 'post_mime_type' => 'image/jpeg',
+ )
+ );
+ $invalid_id = self::factory()->attachment->create(
+ array(
+ 'file' => 'document.pdf',
+ 'post_mime_type' => 'application/pdf',
+ )
+ );
+ $image_ids = array();
+ for ( $i = 1; $i <= 7; $i++ ) {
+ $image_ids[] = self::factory()->attachment->create(
+ array(
+ 'file' => "gallery-$i.jpg",
+ 'post_mime_type' => 'image/jpeg',
+ )
+ );
+ }
+ $duplicate_id = self::factory()->attachment->create(
+ array(
+ 'file' => 'gallery-1.jpg',
+ 'post_mime_type' => 'image/jpeg',
+ )
+ );
+ $this->product->set_image_id( $invalid_id );
+ $this->product->set_gallery_image_ids( array_merge( array( $deleted_id, $image_ids[0], $duplicate_id ), array_slice( $image_ids, 1 ) ) );
+ $this->product->save();
+ wp_delete_attachment( $deleted_id, true );
+
+ $tags = $this->render_meta_tags( '' );
+
+ $this->assertSame(
+ array_map( 'wp_get_attachment_url', array_slice( $image_ids, 0, 6 ) ),
+ $tags['og:image'],
+ 'Invalid and duplicate images should not consume the six-image limit.'
+ );
+ }
+
/**
* @testdox Should render sale prices and sale dates in product metadata.
*/