Commit 36f9a06cb63 for woocommerce
commit 36f9a06cb63a9f5a57ea9ad2763d6a31d9cc3862
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date: Thu Nov 19 22:15:33 2026 +0100
Agent readiness: run the shipping and buy action checks (#71101)
* Agent readiness: run the shipping and buy action checks
Core now prints shippingDetails (#70688) and a BuyAction (#70685) in product
structured data, so both checks run instead of showing as coming soon. The
PENDING list is gone; only UCP checkout still waits, on its own condition.
Each check passes when the markup has the field, whoever printed it, and is
"couldn't check" when the page has no Product JSON-LD. Without the field:
- Shipping details fail when no shipping zone has a delivery method, which
the merchant can add, or when WooCommerce would have printed them. They
are "couldn't check" when WooCommerce wouldn't: a product with no price,
one that isn't simple or doesn't ship, or a store ShippingDestinations
can't describe (shipping off, no countries, a zone criteria filter).
- The buy action is "couldn't check" outside the conditions of
get_product_checkout_url(): a visible, publicly viewable, purchasable,
in-stock simple product.
A test serves the JSON-LD WooCommerce prints for each product state and
asserts the checks never fail on it, so the copied conditions can't drift.
* Say local pickup is not a delivery method on the shipping check.
Pin the shipping and buy-action checks to the JSON-LD WooCommerce prints for a virtual, out-of-stock, and variable product.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Add readiness copy for shipping and buy-action checks
Remove the obsolete PENDING constant reader from ReadinessCopyTest now that those checks are active.
* Link the shipping structured data fix to the Shipping settings
The fix line tells the merchant to add a shipping zone, but the row had
no link since the check stopped being pending.
* Hide the shipping and buy action rows while product details are missing
Both checks read the product structured data, so without it they showed
two extra "couldn't be checked" rows for the same cause.
---------
diff --git a/plugins/woocommerce/changelog/fix-agent-readiness-unpend-shipping-buy-action b/plugins/woocommerce/changelog/fix-agent-readiness-unpend-shipping-buy-action
new file mode 100644
index 00000000000..f0977160336
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-agent-readiness-unpend-shipping-buy-action
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Agent readiness: run the shipping and buy action checks now that core prints them, behind an experimental flag; nothing ships to merchants.
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/README.md b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
index f3c39e6397e..41319b835df 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/README.md
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/README.md
@@ -92,8 +92,8 @@ Store-wide conditions (`StoreConditions`) are reported first because they affect
| `ucp_profile_version` | With UCP on: the served profile declares the UCP version WooCommerce implements (`UcpUtils::VERSION`). A different version usually means another plugin serves its own profile. |
| `ucp_checkout` | With UCP on: the served profile declares `dev.ucp.shopping.checkout`. Pending until this WooCommerce serves UCP checkout; not shown while guest checkout is off, since the profile then offers none. |
| `product_return_policy_schema` | A product offer, or the store's OnlineStore or Organization item, in the product page's JSON-LD has `hasMerchantReturnPolicy`. |
-| `product_shipping_schema` | A product offer has `shippingDetails`. "Couldn't check" for a product that doesn't ship. Pending (WOOPRD-3908). |
-| `product_buy_action_schema` | The product or an offer has a `BuyAction` in `potentialAction`. "Couldn't check" for a variable product without one: WooCommerce links only a chosen variation to checkout (WOOPRD-3946, after the ProductGroup work in WOOPRD-3907). Pending (WOOPRD-3918). |
+| `product_shipping_schema` | A product offer has `shippingDetails`. Without them it fails when no shipping zone has a delivery method (local pickup doesn't count), which the merchant can add, or when WooCommerce would print them. It's "couldn't check" when WooCommerce wouldn't: a product with no price, one that isn't simple (only a chosen variation gets them) or doesn't ship, or a store WooCommerce can't describe the shipping of (shipping off, no shipping countries, or a zone criteria filter). |
+| `product_buy_action_schema` | The product or an offer has a `BuyAction` in `potentialAction`. "Couldn't check" without one for a variable product, or one not purchasable or out of stock: WooCommerce links only a chosen variation, and only a product in stock, to checkout (WOOPRD-3946, after the ProductGroup work in WOOPRD-3907). |
A likely conflict names the page where it was seen (`seen_at`), not a plugin: a list of plugins goes stale, and several plugins can change the same output (WOOAI-1618).
@@ -104,7 +104,7 @@ The checks judge what the store returns, whoever provides it. Some things they c
- Values added in the browser. The page is read as the server sends it, so JSON-LD a script adds after load is missed.
- Every product. Output checks read one product page; the stored-data checks run on every product.
-A check listed in `StoreOutputChecks::PENDING` tests output WooCommerce doesn't build yet. It doesn't run: it's reported with `ok` null and `pending` true, so it reads as coming soon, not as failed or couldn't check. Remove its entry when that output ships.
+`ucp_checkout` tests output WooCommerce doesn't build yet. Until this WooCommerce serves UCP checkout it doesn't run: it's reported with `ok` null and `pending` true, so it reads as coming soon, not as failed or couldn't check.
A check listed in `ReadinessCheckPage::SAME_CAUSE` isn't shown while the setup step it names fails, so one cause shows once: `no_noindex_directives` while "Discourage search engines" is on, `product_return_policy_schema` while no published refund and returns page is chosen, and `ucp_checkout` while guest checkout is off. A stock store creates that page as a draft, so the setup step shows instead.
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCheckPage.php b/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCheckPage.php
index 8dbe73ec8a8..9ca4b07e7f9 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCheckPage.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCheckPage.php
@@ -56,6 +56,8 @@ class ReadinessCheckPage implements RegisterHooksInterface {
'product_jsonld_required_fields' => 'product_jsonld_present',
'product_offer_jsonld' => 'product_jsonld_present',
'product_return_policy_schema' => 'product_jsonld_present',
+ 'product_shipping_schema' => 'product_jsonld_present',
+ 'product_buy_action_schema' => 'product_jsonld_present',
'llms_txt_valid' => 'llms_txt_present',
'llms_txt_links_resolve' => 'llms_txt_present',
);
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCopy.php b/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCopy.php
index 662d65127bc..337d794774e 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCopy.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/ReadinessCopy.php
@@ -341,6 +341,21 @@ final class ReadinessCopy {
__( 'AI assistants compare price and availability before they suggest a product. We found a recent product page without one of them.', 'woocommerce' ),
__( 'Set a price and stock status on the product. If it has them, look at the schema settings in your SEO plugin or theme.', 'woocommerce' ),
),
+ 'product_shipping_schema' => array(
+ __( 'Product structured data gives shipping details', 'woocommerce' ),
+ __( 'Add shipping details to your product structured data', 'woocommerce' ),
+ __( 'Agents tell shoppers the delivery options and cost before they buy.', 'woocommerce' ),
+ __( 'Agents tell shoppers the delivery options and cost before they buy. We found a recent product page without shipping details in its structured data.', 'woocommerce' ),
+ __( 'Add a shipping zone with a delivery method other than local pickup, so WooCommerce can add shipping details to your products. If you already have one, check whether your theme or an SEO plugin replaces WooCommerce\'s structured data.', 'woocommerce' ),
+ admin_url( 'admin.php?page=wc-settings&tab=shipping' ),
+ ),
+ 'product_buy_action_schema' => array(
+ __( 'Product structured data says how to buy', 'woocommerce' ),
+ __( 'Add a buy action to your product structured data', 'woocommerce' ),
+ __( 'A BuyAction tells agents where to send a shopper to buy the product.', 'woocommerce' ),
+ __( 'A BuyAction tells agents where to send a shopper to buy the product. We found a recent product page without one in its structured data.', 'woocommerce' ),
+ __( 'Check whether another plugin replaces WooCommerce\'s product structured data.', 'woocommerce' ),
+ ),
'product_gtin_jsonld' => array(
__( 'Product details have the barcode number', 'woocommerce' ),
__( 'Check why product details have no barcode number', 'woocommerce' ),
diff --git a/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php b/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
index b48b46c0955..6a1325733c3 100644
--- a/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
+++ b/plugins/woocommerce/src/Internal/AgentReadiness/StoreOutputChecks.php
@@ -3,9 +3,12 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Internal\AgentReadiness;
+use Automattic\WooCommerce\Enums\CatalogVisibility;
use Automattic\WooCommerce\Enums\ProductStatus;
+use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\Internal\ComingSoon\ComingSoonHelper;
use Automattic\WooCommerce\Internal\LlmsTxt\LlmsTxt;
+use Automattic\WooCommerce\Internal\StructuredData\ShippingDestinations;
use Automattic\WooCommerce\StoreApi\Utilities\Ucp\Utils as UcpUtils;
defined( 'ABSPATH' ) || exit;
@@ -78,7 +81,8 @@ class StoreOutputChecks {
if ( StoreConditions::is_off( $check['id'] ) ) {
continue;
}
- $pending = in_array( $check['id'], self::PENDING, true ) || ( 'ucp_checkout' === $check['id'] && ! StoreConditions::serves_ucp_checkout() );
+ // UCP checkout doesn't run until this WooCommerce serves it, and reads as coming soon, not failed.
+ $pending = 'ucp_checkout' === $check['id'] && ! StoreConditions::serves_ucp_checkout();
try {
$ok = $pending ? null : ( $check['test'] )( $responses );
} catch ( \Throwable $e ) {
@@ -117,17 +121,6 @@ class StoreOutputChecks {
return $results;
}
- /**
- * Checks for output WooCommerce doesn't build yet. They don't run and are reported as
- * pending, not failed; remove the entry when the output ships. The output is built by
- * WOOPRD-3908 (shipping) and WOOPRD-3918 (buy action). The UCP checkout check is pending
- * until this WooCommerce serves UCP checkout (StoreConditions::serves_ucp_checkout()).
- */
- private const PENDING = array(
- 'product_shipping_schema',
- 'product_buy_action_schema',
- );
-
private const MAX_LISTED_SITEMAPS = 5;
private const MAX_CHILD_SITEMAPS = 5;
@@ -471,18 +464,18 @@ class StoreOutputChecks {
array(
'id' => 'product_shipping_schema',
'group' => 'buy',
- 'test' => fn( StoreResponses $responses ): ?bool => $this->with_products( $responses, static fn( array $products ): ?bool => self::ships( $responses ) ? self::offer_has( $products, 'shippingDetails' ) : null ),
+ 'test' => fn( StoreResponses $responses ): ?bool => $this->with_products( $responses, static fn( array $products ): ?bool => array() === $products ? null : ( self::offer_has( $products, 'shippingDetails' ) ? true : ( null === self::ships( $responses ) ? null : false ) ) ),
'blocks' => 'checkout',
'name' => __( 'Product structured data gives shipping details', 'woocommerce' ),
'label' => __( 'Product structured data has no shipping details', 'woocommerce' ),
'why' => __( 'Agents tell shoppers the delivery options and cost before they buy.', 'woocommerce' ),
- 'fix' => __( 'Set up shipping zones so WooCommerce can add shipping details to your products.', 'woocommerce' ),
+ 'fix' => __( 'Add a shipping zone with a delivery method other than local pickup, so WooCommerce can add shipping details to your products. If you already have one, check whether your theme or an SEO plugin replaces WooCommerce\'s structured data.', 'woocommerce' ),
'fix_url' => admin_url( 'admin.php?page=wc-settings&tab=shipping' ),
),
array(
'id' => 'product_buy_action_schema',
'group' => 'buy',
- 'test' => fn( StoreResponses $responses ): ?bool => $this->with_products( $responses, static fn( array $products ): ?bool => self::has_buy_action( $products, $responses->product() ) ),
+ 'test' => fn( StoreResponses $responses ): ?bool => $this->with_products( $responses, static fn( array $products ): ?bool => array() === $products ? null : self::has_buy_action( $products, $responses->product() ) ),
'blocks' => 'checkout',
'name' => __( 'Product structured data says how to buy', 'woocommerce' ),
'label' => __( 'Product structured data has no buy action', 'woocommerce' ),
@@ -1116,13 +1109,43 @@ class StoreOutputChecks {
}
/**
- * Whether the scanned product needs shipping. A product that doesn't ship has no shipping details to give.
+ * Whether WooCommerce would print shipping details for the scanned product: a simple product with a
+ * price that needs shipping (the conditions in WC_Structured_Data::generate_product_data()), on a store
+ * ShippingDestinations can describe. False when the store has no zone with a delivery method other than
+ * local pickup, which the merchant can fix. Null when WooCommerce wouldn't print them for another reason:
+ * a variable product, shipping off, a zone criteria filter, or zones it can't describe.
*
* @param StoreResponses $responses Store responses.
*/
- private static function ships( StoreResponses $responses ): bool {
+ private static function ships( StoreResponses $responses ): ?bool {
$product = $responses->product();
- return null === $product || $product->needs_shipping();
+ if ( null !== $product && ( '' === $product->get_price() || ! $product->is_type( ProductType::SIMPLE ) || ! $product->needs_shipping() ) ) {
+ return null;
+ }
+ if ( ! wc_shipping_enabled() || has_filter( 'woocommerce_get_zone_criteria' ) ) {
+ return null;
+ }
+ if ( ! self::has_delivery_method() ) {
+ return false;
+ }
+ return array() !== wc_get_container()->get( ShippingDestinations::class )->get_shipping_details() ? true : null;
+ }
+
+ /**
+ * Whether a shipping zone, "Rest of the world" included, has an enabled method other than local pickup,
+ * the methods ShippingDestinations describes.
+ */
+ private static function has_delivery_method(): bool {
+ $zones = \WC_Shipping_Zones::get_shipping_zones();
+ $zones[0] = new \WC_Shipping_Zone( 0 );
+ foreach ( $zones as $zone ) {
+ foreach ( $zone->get_shipping_methods( true ) as $method ) {
+ if ( $method instanceof \WC_Shipping_Method && 'local_pickup' !== $method->id && ! $method->supports( 'local-pickup' ) ) {
+ return true;
+ }
+ }
+ }
+ return false;
}
/**
@@ -1236,8 +1259,8 @@ class StoreOutputChecks {
/**
* Whether a Product item, a variant or one of their offers has a BuyAction potentialAction.
- * Null for a variable product without one: WooCommerce doesn't link a variable product's
- * page to checkout, only a chosen variation.
+ * Null without one when WooCommerce wouldn't print it: it links only a simple product, or a chosen
+ * variation, that can be bought now (the conditions of WC_Structured_Data::get_product_checkout_url()).
*
* @param array<int, array<string, mixed>> $products Product items.
* @param \WC_Product|null $scanned The scanned product.
@@ -1253,7 +1276,10 @@ class StoreOutputChecks {
}
}
}
- return null !== $scanned && $scanned->is_type( 'variable' ) ? null : false;
+ if ( null !== $scanned && ( ! $scanned->is_type( ProductType::SIMPLE ) || ( ! $scanned->is_visible() && CatalogVisibility::SEARCH !== $scanned->get_catalog_visibility() ) || ! $scanned->is_publicly_viewable() || ! $scanned->is_purchasable() || ! $scanned->is_in_stock() || ! $scanned->has_enough_stock( 1 ) ) ) {
+ return null;
+ }
+ return false;
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
index 317e55bdf1b..4d17cf99f35 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/AgenticCommerceSettingsTest.php
@@ -508,6 +508,8 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
)
),
$row( 'product_offer_jsonld', null ),
+ $row( 'product_shipping_schema', null ),
+ $row( 'product_buy_action_schema', null ),
$row( 'sitemap_lists_products', true ),
),
)
@@ -571,6 +573,8 @@ class AgenticCommerceSettingsTest extends WC_Unit_Test_Case {
$this->assertStringNotContainsString( 'Yoast SEO', $json, 'No plugin is named, even from an older stored report' );
$this->assertStringNotContainsString( 'product_offer_jsonld name', $json, 'Its prerequisite failed' );
$this->assertStringNotContainsString( 'product_jsonld_present name', $json );
+ $this->assertStringNotContainsString( 'shipping details', $json, 'The shipping check needs product details too' );
+ $this->assertStringNotContainsString( 'says how to buy', $json, 'and so does the buy action check' );
$this->assertStringNotContainsString( 'sitemap_lists_products label', $json, 'A passed check keeps its passing title' );
$this->assertSame( array(), $sections['find']['rows'], 'Nothing open under find' );
$passed = array_column( $sections['find']['folded'], null, 'title' )['A sitemap lists your products'] ?? null;
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessCopyTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessCopyTest.php
index d19b5ddfea0..8edddb2b7cd 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessCopyTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ReadinessCopyTest.php
@@ -13,23 +13,13 @@ use WC_Unit_Test_Case;
*/
class ReadinessCopyTest extends WC_Unit_Test_Case {
- /**
- * Checks that wait for output WooCommerce doesn't build yet, so Sales readiness doesn't show them.
- *
- * @return array<int, string>
- */
- private static function pending(): array {
- return ( new \ReflectionClassConstant( StoreOutputChecks::class, 'PENDING' ) )->getValue();
- }
-
-
/**
* @testdox Every store check Sales readiness shows has copy, headed by what to do and saying what the scan found.
*/
public function test_every_store_check_has_copy(): void {
$checks = new \ReflectionMethod( StoreOutputChecks::class, 'checks' );
$checks->setAccessible( true );
- $ids = array_diff( array_column( $checks->invoke( wc_get_container()->get( StoreOutputChecks::class ) ), 'id' ), self::pending() );
+ $ids = array_column( $checks->invoke( wc_get_container()->get( StoreOutputChecks::class ) ), 'id' );
$this->assertNotEmpty( $ids );
foreach ( $ids as $id ) {
@@ -71,9 +61,10 @@ class ReadinessCopyTest extends WC_Unit_Test_Case {
public function test_every_store_check_has_a_fix_line(): void {
$checks = new \ReflectionMethod( StoreOutputChecks::class, 'checks' );
$checks->setAccessible( true );
- foreach ( array_diff( array_column( $checks->invoke( wc_get_container()->get( StoreOutputChecks::class ) ), 'id' ), self::pending() ) as $id ) {
+ foreach ( array_column( $checks->invoke( wc_get_container()->get( StoreOutputChecks::class ) ), 'id' ) as $id ) {
$this->assertNotSame( '', ReadinessCopy::store_check( $id )['fix'] ?? '', $id );
}
+ $this->assertStringContainsString( 'tab=shipping', ReadinessCopy::store_check( 'product_shipping_schema' )['url'], 'The shipping fix links to the Shipping settings' );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ServedOutputChecksTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ServedOutputChecksTest.php
index 76f210fd84d..0c359a7c5db 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ServedOutputChecksTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/ServedOutputChecksTest.php
@@ -407,7 +407,7 @@ class ServedOutputChecksTest extends WC_Unit_Test_Case {
}
/**
- * @testdox Checks for output WooCommerce doesn't build yet don't run, and are reported as pending.
+ * @testdox Only the UCP checkout check waits as pending, until this WooCommerce serves UCP checkout; the product structured data checks run.
*/
public function test_pending_checks(): void {
update_option( 'woocommerce_feature_ucp_enabled', 'yes' );
@@ -415,16 +415,62 @@ class ServedOutputChecksTest extends WC_Unit_Test_Case {
$checks = array_column( $this->sut->all(), null, 'id' );
- foreach ( ( new \ReflectionClassConstant( StoreOutputChecks::class, 'PENDING' ) )->getValue() as $id ) {
- $this->assertNull( $checks[ $id ]['ok'], $id );
- $this->assertTrue( $checks[ $id ]['pending'], $id );
+ foreach ( array( 'open_graph_tags', 'product_return_policy_schema', 'product_shipping_schema', 'product_buy_action_schema' ) as $id ) {
+ $this->assertFalse( $checks[ $id ]['pending'], $id . ' runs: WooCommerce prints its output' );
}
- $this->assertFalse( $checks['open_graph_tags']['pending'] );
- $this->assertFalse( $checks['product_return_policy_schema']['pending'], 'WooCommerce adds the returns policy since 11.3' );
- $this->assertContains( 'product_shipping_schema', ( new \ReflectionClassConstant( StoreOutputChecks::class, 'PENDING' ) )->getValue(), 'The list is not empty' );
$this->assertSame( ! StoreConditions::serves_ucp_checkout(), $checks['ucp_checkout']['pending'], 'The UCP checkout check runs once this WooCommerce serves UCP checkout' );
}
+ /**
+ * @testdox The shipping and buy action checks read the product page, and couldn't check a product that can't ship or can't be bought now.
+ */
+ public function test_shipping_and_buy_action_on_the_page(): void {
+ $ld = static fn( string $offer ): string => '<script type="application/ld+json">{"@context":"https://schema.org","@graph":[{"@type":"Product","name":"Mug","offers":' . $offer . '}]}</script>';
+ $responses = $this->good_store();
+ $result = function ( string $offer, string $id ) use ( $responses, $ld ): ?bool {
+ $responses[ $this->product_path() ] = array( 200, self::page( $ld( $offer ) ) );
+ $this->serve( $responses );
+ return array_column( $this->sut->all(), 'ok', 'id' )[ $id ];
+ };
+ $bare = '{"@type":"Offer","price":"10"}';
+ $zone = new \WC_Shipping_Zone();
+ $zone->set_zone_name( 'US' );
+ $zone->add_location( 'US', 'country' );
+ $zone->save();
+ $zone->add_shipping_method( 'flat_rate' );
+
+ $this->assertTrue( $result( '{"@type":"Offer","price":"10","shippingDetails":{"@type":"OfferShippingDetails"}}', 'product_shipping_schema' ) );
+ $this->assertFalse( $result( $bare, 'product_shipping_schema' ) );
+ $this->assertFalse( $result( $bare, 'product_buy_action_schema' ), 'An in-stock simple product gets a BuyAction from WooCommerce' );
+ $responses[ $this->product_path() ] = array( 200, self::page( '' ) );
+ $this->serve( $responses );
+ $no_ld = array_column( $this->sut->all(), 'ok', 'id' );
+ $this->assertNull( $no_ld['product_shipping_schema'], 'Without product details it couldn\'t check, as the other product details checks' );
+ $this->assertNull( $no_ld['product_buy_action_schema'] );
+
+ update_option( 'woocommerce_ship_to_countries', 'disabled' );
+ $this->assertNull( $result( $bare, 'product_shipping_schema' ), 'Shipping turned off: no shipping details to give' );
+ delete_option( 'woocommerce_ship_to_countries' );
+ add_filter( 'woocommerce_get_zone_criteria', '__return_empty_array' );
+ $this->assertNull( $result( $bare, 'product_shipping_schema' ), 'A zone criteria filter: WooCommerce prints no shipping details' );
+ remove_filter( 'woocommerce_get_zone_criteria', '__return_empty_array' );
+
+ add_filter( 'woocommerce_countries_shipping_countries', '__return_empty_array' );
+ $this->assertNull( $result( $bare, 'product_shipping_schema' ), 'A zone WooCommerce can\'t describe: it prints no shipping details' );
+ remove_filter( 'woocommerce_countries_shipping_countries', '__return_empty_array' );
+ $zone->delete();
+ $this->assertFalse( $result( $bare, 'product_shipping_schema' ), 'No zone with a delivery method: the merchant can add one' );
+ $this->assertTrue( $result( '{"@type":"Offer","price":"10","shippingDetails":{"@type":"OfferShippingDetails"}}', 'product_shipping_schema' ), 'Printed by another plugin, it still counts' );
+
+ add_filter( 'woocommerce_product_is_visible', '__return_false' );
+ $this->assertNull( $result( $bare, 'product_buy_action_schema' ), 'Hidden by a visibility filter: WooCommerce prints no BuyAction' );
+ remove_filter( 'woocommerce_product_is_visible', '__return_false' );
+
+ $this->product->set_stock_status( 'outofstock' );
+ $this->product->save();
+ $this->assertNull( $result( $bare, 'product_buy_action_schema' ), 'WooCommerce links only a product in stock to checkout' );
+ }
+
/**
* @testdox The returns policy check passes for a policy on an offer or on the store's item, as WooCommerce adds it, fails without one, and couldn't check without product details.
*/
@@ -445,7 +491,7 @@ class ServedOutputChecksTest extends WC_Unit_Test_Case {
}
/**
- * @testdox The pending product checks find a returns policy and shipping details on an offer, and a BuyAction on the product or an offer.
+ * @testdox The product checks find a returns policy and shipping details on an offer, and a BuyAction on the product or an offer.
*/
public function test_pending_product_detectors(): void {
$offer_has = new \ReflectionMethod( StoreOutputChecks::class, 'offer_has' );
@@ -530,11 +576,26 @@ class ServedOutputChecksTest extends WC_Unit_Test_Case {
$this->assertNull( $buy_action->invoke( null, array( $empty_group ), $variable ), 'A variable product without one' );
$this->assertFalse( $buy_action->invoke( null, array( $empty_group ), $this->product ), 'A simple product without one' );
+ $variable->delete( true );
+ $this->assertFalse( $ships->invoke( null, new StoreResponses( true ) ), 'No zone with a delivery method' );
+ $zone = new \WC_Shipping_Zone();
+ $zone->add_location( 'US', 'country' );
+ $zone->save();
+ $zone->add_shipping_method( 'local_pickup' );
+ $this->assertFalse( $ships->invoke( null, new StoreResponses( true ) ), 'Local pickup only: WooCommerce describes no delivery' );
+ $zone->add_shipping_method( 'flat_rate' );
$this->assertTrue( $ships->invoke( null, new StoreResponses( true ) ) );
+ $this->product->set_regular_price( '' );
+ $this->product->save();
+ $this->assertNull( $ships->invoke( null, new StoreResponses( true ) ), 'Without a price WooCommerce prints no offer' );
+ $this->product->set_regular_price( '10' );
$this->product->set_virtual( true );
$this->product->save();
- $variable->delete( true );
- $this->assertFalse( $ships->invoke( null, new StoreResponses( true ) ), 'A virtual product has no shipping details to give' );
+ $this->assertNull( $ships->invoke( null, new StoreResponses( true ) ), 'A virtual product has no shipping details to give' );
+
+ $this->product->delete( true );
+ WC_Helper_Product::create_variation_product();
+ $this->assertNull( $ships->invoke( null, new StoreResponses( true ) ), 'A variable product: WooCommerce prints shipping details only for a chosen variation' );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
index 0887e86f091..336281eba26 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AgentReadiness/StoreOutputChecksTest.php
@@ -3,6 +3,7 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\AgentReadiness;
+use Automattic\WooCommerce\Enums\ProductStockStatus;
use Automattic\WooCommerce\Internal\AgentReadiness\JsonLd;
use Automattic\WooCommerce\Internal\AgentReadiness\RemoteRules;
use Automattic\WooCommerce\Internal\AgentReadiness\StoreOutputChecks;
@@ -412,6 +413,47 @@ class StoreOutputChecksTest extends WC_Unit_Test_Case {
$this->assertSame( array( true, true, true ), $this->product_results() );
}
+ /**
+ * @testdox The shipping and buy action checks match the JSON-LD WooCommerce prints for each product state.
+ *
+ * Shipping details, then the buy action. A virtual product still gets a buy action. An out-of-stock
+ * product still gets shipping details. A variable product page gets neither.
+ *
+ * @testWith ["shipped", [true, true]]
+ * ["virtual", [null, true]]
+ * ["out_of_stock", [true, null]]
+ * ["variable", [null, null]]
+ *
+ * @param string $state Product state.
+ * @param array<int, bool|null> $expected Shipping check, then the buy action check.
+ */
+ public function test_core_jsonld_matches_what_woocommerce_prints( string $state, array $expected ): void {
+ $zone = new \WC_Shipping_Zone();
+ $zone->add_location( 'US', 'country' );
+ $zone->save();
+ $zone->add_shipping_method( 'flat_rate' );
+ $product = 'variable' === $state ? WC_Helper_Product::create_variation_product() : WC_Helper_Product::create_simple_product();
+ if ( 'virtual' === $state ) {
+ $product->set_virtual( true );
+ } elseif ( 'out_of_stock' === $state ) {
+ $product->set_stock_status( ProductStockStatus::OUT_OF_STOCK );
+ }
+ $product->save();
+ if ( $product instanceof \WC_Product_Variable ) {
+ \WC_Product_Variable::sync( $product );
+ }
+ $product = wc_get_product( $product->get_id() );
+ $this->serve( array( self::path_of( $product ) => array( 200, self::page_with( array( self::core_product_jsonld( $product ) ) ) ) ) );
+ $results = $this->results();
+
+ $this->assertSame(
+ $expected,
+ array( $results['product_shipping_schema'], $results['product_buy_action_schema'] ),
+ 'These checks disagree with the JSON-LD WooCommerce printed: their copy of the print conditions has drifted from WC_Structured_Data'
+ );
+ $zone->delete();
+ }
+
/**
* @testdox The product page checks skip a newer product of a type the report doesn't check.
*/