Commit 8cac9993195 for woocommerce

commit 8cac99931955a660b17fc751acb1e338551c1f4c
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date:   Thu Nov 19 14:48:47 2026 +0530

    Fix missing product structured data (#68845)

    * Fix missing product structured data

    * Add changelog entry for product structured data fix

    * Fix fallback Product schema for queried product

    * Fix product schema fallback for protected products and removed hooks

    * Simplify product structured data footer fallback

    * Fix identifier-free product schema detection in footer

    * Restore structured data setter compatibility

    * Simplify product structured data generation tracking

    * Simplify structured data dedupe to a single property

    Drop $generating_product_id and the try/finally around set_data(). generate_product_data() now marks the product in $generated_product_ids right after storing its data, so set_data() is back to its original behavior. Remove the retry-after-reject and regenerate-after-reset tests, which only covered the removed property.

    * Remove structured data tests that no longer add coverage

    These three tests were written for earlier versions of the fix, which
    matched Product nodes by @id, url or @type, or changed the set_data()
    signature. Tracking now uses the product ID and set_data() is unchanged,
    so the remaining tests catch the same mistakes.

    Amp-Thread-ID: https://ampcode.com/threads/T-9d52df21-2654-419c-881e-a3de95f11ac7
    Co-authored-by: Amp <amp@ampcode.com>

    ---------

    Co-authored-by: Tung Du <tung.du@automattic.com>
    Co-authored-by: Amp <amp@ampcode.com>

diff --git a/plugins/woocommerce/changelog/53242-product-structured-data b/plugins/woocommerce/changelog/53242-product-structured-data
new file mode 100644
index 00000000000..9b2b515594f
--- /dev/null
+++ b/plugins/woocommerce/changelog/53242-product-structured-data
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Preserve product structured data when single product templates omit the Product Summary block.
diff --git a/plugins/woocommerce/includes/class-wc-structured-data.php b/plugins/woocommerce/includes/class-wc-structured-data.php
index 918d6d204bc..7b3c93d1f04 100644
--- a/plugins/woocommerce/includes/class-wc-structured-data.php
+++ b/plugins/woocommerce/includes/class-wc-structured-data.php
@@ -38,6 +38,13 @@ class WC_Structured_Data {
 	 */
 	private $_data = array();

+	/**
+	 * Product IDs whose structured data has been stored.
+	 *
+	 * @var array
+	 */
+	private $generated_product_ids = array();
+
 	/**
 	 * Constructor.
 	 */
@@ -193,6 +200,18 @@ class WC_Structured_Data {
 	 * Hooked into `woocommerce_email_order_details` action hook.
 	 */
 	public function output_structured_data() {
+		if ( 'wp_footer' === current_action() && is_product() && false !== has_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ) ) ) {
+			$product_id       = get_queried_object_id();
+			$has_product_data = isset( $this->generated_product_ids[ $product_id ] );
+
+			if ( ! $has_product_data ) {
+				$product = wc_get_product( $product_id );
+				if ( $product ) {
+					$this->generate_product_data( $product );
+				}
+			}
+		}
+
 		$types = $this->get_data_type_for_page();
 		$data  = $this->get_structured_data( $types );

@@ -232,7 +251,7 @@ class WC_Structured_Data {
 			global $product;
 		}

-		if ( ! is_a( $product, 'WC_Product' ) ) {
+		if ( ! is_a( $product, 'WC_Product' ) || '' !== $product->get_post_password() ) {
 			return;
 		}

@@ -678,7 +697,15 @@ class WC_Structured_Data {
 			$markup['subjectOf'] = $videos;
 		}

+		/**
+		 * Filters structured data for a product.
+		 *
+		 * @since 3.0.0
+		 * @param array      $markup  Product structured data.
+		 * @param WC_Product $product Product being described.
+		 */
 		$this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );
+		$this->generated_product_ids[ $product->get_id() ] = true;
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
index 1de634d5454..6ea7a4bbfb0 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
@@ -23,6 +23,19 @@ class WC_Structured_Data_Test extends \WC_Unit_Test_Case {
 		$this->structured_data = new WC_Structured_Data();
 	}

+	/**
+	 * Outputs structured data through the footer hook.
+	 *
+	 * @return string
+	 */
+	private function output_footer_structured_data(): string {
+		// The test bootstrap retains this deprecated core callback, which is unrelated to structured data.
+		remove_action( 'wp_footer', 'the_block_template_skip_link' );
+		ob_start();
+		do_action( 'wp_footer' );
+		return ob_get_clean();
+	}
+
 	/**
 	 * @testdox get_structured_data() preserves scalar @type grouping.
 	 */
@@ -352,6 +365,94 @@ class WC_Structured_Data_Test extends \WC_Unit_Test_Case {
 		$this->assertEquals( $this->structured_data->prepare_gtin( '123.4e-5' ), '12345' );
 	}

+	/**
+	 * @testdox Product structured data is generated when the product template omits the summary action.
+	 */
+	public function test_output_structured_data_generates_missing_product_data_in_footer(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$this->go_to( get_permalink( $product->get_id() ) );
+
+		$this->output_footer_structured_data();
+
+		$this->assertSame(
+			'Product',
+			$this->structured_data->get_data()[0]['@type'] ?? null,
+			'The queried product should have structured data even when the summary action did not run.'
+		);
+	}
+
+	/**
+	 * @testdox Direct output outside the footer does not generate product data.
+	 */
+	public function test_output_structured_data_does_not_generate_product_data_outside_footer(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$this->go_to( get_permalink( $product->get_id() ) );
+
+		ob_start();
+		$this->structured_data->output_structured_data();
+		$output = ob_get_clean();
+
+		$this->assertSame( array(), $this->structured_data->get_data(), 'Direct output must not generate product data outside the footer.' );
+		$this->assertStringNotContainsString( '"Product"', $output, 'Direct output must not include Product schema.' );
+	}
+
+	/**
+	 * @testdox Password-protected products do not generate structured data from either the summary or the footer.
+	 */
+	public function test_password_protected_product_does_not_generate_product_data(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_post_password( 'secret' );
+		$product->save();
+		$this->go_to( get_permalink( $product->get_id() ) );
+
+		$this->structured_data->generate_product_data( $product );
+		$this->output_footer_structured_data();
+
+		$this->assertSame( array(), $this->structured_data->get_data(), 'Password-protected products must not expose their details in structured data.' );
+	}
+
+	/**
+	 * @testdox Removing the product summary callback also disables the footer fallback.
+	 */
+	public function test_output_structured_data_respects_removed_summary_callback(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$this->go_to( get_permalink( $product->get_id() ) );
+		remove_action( 'woocommerce_single_product_summary', array( $this->structured_data, 'generate_product_data' ), 60 );
+
+		$this->output_footer_structured_data();
+
+		$this->assertSame( array(), $this->structured_data->get_data(), 'The fallback must respect extensions that disable product schema generation.' );
+	}
+
+	/**
+	 * @testdox Product structured data is not duplicated when the product summary already generated it.
+	 */
+	public function test_output_structured_data_does_not_duplicate_existing_product_data(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$this->go_to( get_permalink( $product->get_id() ) );
+		$this->structured_data->generate_product_data( $product );
+
+		$this->output_footer_structured_data();
+
+		$this->assertCount( 1, $this->structured_data->get_data(), 'The fallback should not duplicate existing product data.' );
+	}
+
+	/**
+	 * @testdox Product structured data for another product does not prevent generating data for the queried product.
+	 */
+	public function test_output_structured_data_ignores_other_product_data(): void {
+		$product       = WC_Helper_Product::create_simple_product();
+		$other_product = WC_Helper_Product::create_simple_product();
+		$this->go_to( get_permalink( $product->get_id() ) );
+		$this->structured_data->generate_product_data( $other_product );
+
+		$this->output_footer_structured_data();
+
+		$data = $this->structured_data->get_data();
+		$this->assertCount( 2, $data, 'The fallback should generate data for the queried product.' );
+		$this->assertSame( get_permalink( $product->get_id() ) . '#product', $data[1]['@id'], 'The second Product node should describe the queried product.' );
+	}
+
 	/**
 	 * @testdox Product schema includes unique gallery images after the featured image.
 	 */