Commit d9b152a965f for woocommerce
commit d9b152a965fd66f7bf8fb44fcabc1b926d4f390c
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date: Wed Nov 18 02:23:53 2026 +0100
Let AI shopping apps open "Search results only" products (#70913)
* Let AI shopping apps open "Search results only" products
Catalog search returns them, but lookup and product details refused them:
is_visible() accepts "Search results only" only on a storefront search page.
They are now checked as if shown everywhere, keeping the other visibility rules
and the woocommerce_product_is_visible filter.
* Cover category search and variation IDs for "Search results only" products
Fix the stale visibility comment, note that the filter sees the saved setting, and test a category search with no query and a lookup by variation ID.
* UCP catalog: check Search results only without set_catalog_visibility
Avoid WC_Data_Exception when an extension removes visible from product
visibility options; mirror is_visible core rules for search-only products.
diff --git a/plugins/woocommerce/changelog/fix-ucp-search-only-visibility b/plugins/woocommerce/changelog/fix-ucp-search-only-visibility
new file mode 100644
index 00000000000..453a820063a
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-ucp-search-only-visibility
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+AI shopping apps can now open products set to "Search results only", which they could already find by search.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCatalogRoute.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCatalogRoute.php
index ed6a3b4b394..0d34d47dbe7 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCatalogRoute.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Ucp/AbstractCatalogRoute.php
@@ -3,6 +3,8 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\StoreApi\Routes\V1\Ucp;
+use Automattic\WooCommerce\Enums\CatalogVisibility;
+use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Internal\ProductFeed\Mapping\UcpProductMapper;
use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractRoute;
use Automattic\WooCommerce\StoreApi\Schemas\V1\Ucp\UcpProductSchema;
@@ -178,9 +180,9 @@ abstract class AbstractCatalogRoute extends AbstractRoute {
$product = wc_get_product( $product->get_parent_id() );
}
- // Catalog visibility as the storefront applies it: published, not hidden
- // from the catalog, and in stock when the store hides out-of-stock items.
- if ( ! $product instanceof \WC_Product || ! $product->is_visible() ) {
+ // Visibility as catalog search applies it: published, not "Hidden", and in
+ // stock when the store hides out-of-stock items.
+ if ( ! $product instanceof \WC_Product || ! self::is_reachable( $product ) ) {
return null;
}
@@ -201,6 +203,42 @@ abstract class AbstractCatalogRoute extends AbstractRoute {
);
}
+ /**
+ * Whether an agent can open the product by its ID. is_visible() accepts "Search results
+ * only" products only on a storefront search page, but catalog search returns them, so
+ * they are checked as if shown everywhere, keeping the other rules and the filter. The
+ * filter gets the ID, so a callback that reloads the product sees its saved setting.
+ *
+ * Does not call set_catalog_visibility(): an extension can remove `visible` from
+ * woocommerce_product_visibility_options, and that setter would throw on the temporary value.
+ *
+ * @param \WC_Product $product Product, not a variation.
+ * @return bool
+ */
+ private static function is_reachable( \WC_Product $product ): bool {
+ if ( CatalogVisibility::SEARCH !== $product->get_catalog_visibility() ) {
+ return $product->is_visible();
+ }
+ // is_viewable() is true for trash when the current user can edit the product.
+ // A catalog lookup is a public read, so an unpublished product is never reachable.
+ if ( ProductStatus::PUBLISH !== $product->get_status() ) {
+ return false;
+ }
+ // Same core rules as a Shop and search results product, then the public visibility filter.
+ $visible = $product->is_viewable();
+ if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $product->is_in_stock() ) {
+ $visible = false;
+ }
+ /**
+ * Filters the product visibility for catalog lookup (same hook as WC_Product::is_visible).
+ *
+ * @since 2.1.0 Filter introduced on WC_Product::is_visible.
+ * @param bool $visible Whether the product is considered visible.
+ * @param int $product_id Product ID.
+ */
+ return (bool) apply_filters( 'woocommerce_product_is_visible', $visible, $product->get_id() );
+ }
+
/**
* Build `messages` entries for products whose variant list was cut short.
*
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Ucp/CatalogReachabilityTest.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Ucp/CatalogReachabilityTest.php
new file mode 100644
index 00000000000..eb7372ff56b
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Ucp/CatalogReachabilityTest.php
@@ -0,0 +1,50 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Routes\Ucp;
+
+use Automattic\WooCommerce\StoreApi\Routes\V1\Ucp\AbstractCatalogRoute;
+use WC_Helper_Product;
+use WC_Unit_Test_Case;
+
+/**
+ * Catalog lookup reachability for Search results only products.
+ */
+class CatalogReachabilityTest extends WC_Unit_Test_Case {
+ /**
+ * Whether a product is reachable through catalog lookup.
+ *
+ * @param \WC_Product $product Product.
+ */
+ private function reachable( \WC_Product $product ): bool {
+ $method = new \ReflectionMethod( AbstractCatalogRoute::class, 'is_reachable' );
+ $method->setAccessible( true );
+ return (bool) $method->invoke( null, $product );
+ }
+
+ /**
+ * @testdox A published Search results only product can be looked up.
+ */
+ public function test_published_search_only_product_is_reachable(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_catalog_visibility( 'search' );
+ $product->save();
+
+ $this->assertTrue( $this->reachable( wc_get_product( $product->get_id() ) ) );
+ }
+
+ /**
+ * @testdox A trashed Search results only product stays out of catalog lookup, including for a user who can edit it.
+ */
+ public function test_trashed_search_only_product_is_not_reachable_to_an_editor(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_catalog_visibility( 'search' );
+ $product->set_status( 'trash' );
+ $product->save();
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+
+ $loaded = wc_get_product( $product->get_id() );
+ $this->assertInstanceOf( \WC_Product::class, $loaded );
+ $this->assertFalse( $this->reachable( $loaded ) );
+ }
+}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCatalog.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCatalog.php
index e81901c532a..c54460749d3 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCatalog.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/UcpCatalog.php
@@ -194,6 +194,100 @@ class UcpCatalog extends ControllerTestCase {
$this->assertSame( 'not_found', $data['messages'][0]['code'] );
}
+ /**
+ * Lookup and product details open what catalog search can return: every product but a
+ * hidden one, including "Search results only", which is_visible() refuses outside a
+ * storefront search.
+ *
+ * @testWith ["visible", true, true, true]
+ * ["search", true, false, true]
+ * ["catalog", false, true, true]
+ * ["hidden", false, false, false]
+ *
+ * @param string $visibility Catalog visibility.
+ * @param bool $found_by_name Whether a search by name returns it.
+ * @param bool $browsable Whether a category search with no query returns it.
+ * @param bool $opens Whether lookup and product details return it.
+ */
+ public function test_lookup_and_details_open_what_search_can_return( string $visibility, bool $found_by_name, bool $browsable, bool $opens ): void {
+ $product = \WC_Helper_Product::create_simple_product( true, array( 'name' => 'UCP Zebra ' . $visibility ) );
+ $product->set_catalog_visibility( $visibility );
+ $product->save();
+ $id = (string) $product->get_id();
+ $term = wp_insert_term( 'UCP Zebras ' . $visibility, 'product_cat' );
+ wp_set_object_terms( $product->get_id(), (int) $term['term_id'], 'product_cat' );
+
+ $found = wp_list_pluck( $this->post( '/catalog/search', array( 'query' => 'UCP Zebra ' . $visibility ) )->get_data()['products'], 'id' );
+ $this->assertSame( $found_by_name, in_array( $id, $found, true ), 'Search by name' );
+
+ $browsed = wp_list_pluck( $this->post( '/catalog/search', array( 'filters' => array( 'categories' => array( (string) $term['term_id'] ) ) ) )->get_data()['products'], 'id' );
+ $this->assertSame( $browsable, in_array( $id, $browsed, true ), 'Search by category with no query' );
+
+ $lookup = $this->post( '/catalog/lookup', array( 'ids' => array( $id ) ) )->get_data();
+ $this->assertSame( $opens ? array( $id ) : array(), wp_list_pluck( $lookup['products'], 'id' ), 'Lookup' );
+
+ $detail = $this->post( '/catalog/product', array( 'id' => $id ) )->get_data();
+ $this->assertSame( $opens ? $id : null, $detail['product']['id'] ?? null, 'Product details' );
+ }
+
+ /**
+ * A variation ID opens in lookup and product details when its parent is "Search results only".
+ */
+ public function test_lookup_and_details_open_a_variation_of_a_search_only_product(): void {
+ $product = \WC_Helper_Product::create_variation_product();
+ $product->set_catalog_visibility( 'search' );
+ $product->save();
+ $children = $product->get_children();
+ $variation_id = (string) end( $children );
+
+ $data = $this->post( '/catalog/lookup', array( 'ids' => array( $variation_id ) ) )->get_data();
+
+ $this->assertSame( array( (string) $product->get_id() ), wp_list_pluck( $data['products'], 'id' ) );
+ $this->assertContains( $variation_id, wp_list_pluck( $data['products'][0]['variants'], 'id' ) );
+
+ $detail = $this->post( '/catalog/product', array( 'id' => $variation_id ) )->get_data();
+ $this->assertSame( $variation_id, $detail['product']['variants'][0]['id'] ?? null, 'Product details' );
+ }
+
+ /**
+ * A "Search results only" product still follows the hide-out-of-stock setting.
+ */
+ public function test_lookup_hides_out_of_stock_search_only_products_when_the_store_does(): void {
+ $product = \WC_Helper_Product::create_simple_product( true, array( 'stock_status' => 'outofstock' ) );
+ $product->set_catalog_visibility( 'search' );
+ $product->save();
+
+ update_option( 'woocommerce_hide_out_of_stock_items', 'yes' );
+ $data = $this->post( '/catalog/lookup', array( 'ids' => array( (string) $product->get_id() ) ) )->get_data();
+ update_option( 'woocommerce_hide_out_of_stock_items', 'no' );
+
+ $this->assertSame( array(), $data['products'] );
+ }
+
+ /**
+ * Lookup still opens a "Search results only" product when an extension removes `visible`
+ * from the catalog visibility options (the temporary override path must not throw).
+ */
+ public function test_lookup_opens_search_only_when_visible_option_is_removed(): void {
+ $product = \WC_Helper_Product::create_simple_product();
+ $product->set_catalog_visibility( 'search' );
+ $product->save();
+ $id = (string) $product->get_id();
+
+ $filter = static function ( array $options ): array {
+ unset( $options['visible'] );
+ return $options;
+ };
+ add_filter( 'woocommerce_product_visibility_options', $filter );
+ try {
+ $data = $this->post( '/catalog/lookup', array( 'ids' => array( $id ) ) )->get_data();
+ } finally {
+ remove_filter( 'woocommerce_product_visibility_options', $filter );
+ }
+
+ $this->assertSame( array( $id ), wp_list_pluck( $data['products'], 'id' ) );
+ }
+
/**
* A category list resolving to no existing term matches nothing rather than everything.
*/