Commit ad2bf7a11ad for woocommerce
commit ad2bf7a11ad8cdcd33189c1e156c1fdb8863c79e
Author: Bero <1199991+bgrgicak@users.noreply.github.com>
Date: Mon Nov 16 12:25:42 2026 +0100
Add checkout links to product structured data (#70685)
* Add checkout links to product structured data
* Add changelog entry for product checkout links
* Test product schema checkout links
* Use existing checkout link format for variations
* Return selected variation data without references
* Keep existing variation resolver unchanged
* Return null when a checkout link is unavailable
* Remove checkout links for Any variations
* Move checkout schema tests to structured data suite
* Simplify checkout link eligibility checks
* Simplify variation attribute check
* Respect product visibility in checkout links
* Allow checkout links for search-only products
diff --git a/plugins/woocommerce/changelog/add-product-schema-checkout-links b/plugins/woocommerce/changelog/add-product-schema-checkout-links
new file mode 100644
index 00000000000..1893d5feabf
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-product-schema-checkout-links
@@ -0,0 +1,4 @@
+Significance: minor
+Type: enhancement
+
+Enable direct checkout links in supported product listings.
diff --git a/plugins/woocommerce/includes/class-wc-structured-data.php b/plugins/woocommerce/includes/class-wc-structured-data.php
index 9f23a5a4e6e..918d6d204bc 100644
--- a/plugins/woocommerce/includes/class-wc-structured-data.php
+++ b/plugins/woocommerce/includes/class-wc-structured-data.php
@@ -13,6 +13,7 @@
*/
use Automattic\WooCommerce\Internal\ProductGallery\ProductMediaGallery;
+use Automattic\WooCommerce\Enums\CatalogVisibility;
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Enums\ProductType;
@@ -538,7 +539,23 @@ class WC_Structured_Data {
}
}
+ $checkout_url = $this->get_product_checkout_url( $product, $selected_variation );
+ if ( null !== $checkout_url ) {
+ $markup_offer['checkoutPageURLTemplate'] = $checkout_url;
+ }
+
$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
+ $offer = $markup['offers'][0];
+ if ( null !== $checkout_url && is_array( $offer ) && 'Offer' === ( $offer['@type'] ?? null ) && is_string( $offer['checkoutPageURLTemplate'] ?? null ) && '' !== $offer['checkoutPageURLTemplate'] ) {
+ $markup['potentialAction'] = array(
+ '@type' => 'BuyAction',
+ 'target' => array(
+ '@type' => 'EntryPoint',
+ 'urlTemplate' => $offer['checkoutPageURLTemplate'],
+ 'actionPlatform' => array( 'https://schema.org/DesktopWebPlatform', 'https://schema.org/MobileWebPlatform' ),
+ ),
+ );
+ }
}
if ( $product->get_rating_count() && wc_review_ratings_enabled() ) {
@@ -1080,6 +1097,43 @@ class WC_Structured_Data {
return $variation;
}
+ /**
+ * Get a checkout link for a simple product or selected variation.
+ *
+ * @param WC_Product $product Product shown on the page.
+ * @param WC_Product_Variation|null $variation Selected variation, if any.
+ * @return string|null Checkout URL, or null if it cannot be represented.
+ */
+ private function get_product_checkout_url( $product, $variation ) {
+ if ( ! $variation && ! $product->is_type( ProductType::SIMPLE ) ) {
+ return null;
+ }
+
+ $item = $variation ? $variation : $product;
+ if (
+ ( ! $product->is_visible() && CatalogVisibility::SEARCH !== $product->get_catalog_visibility() ) ||
+ ( $variation && ! $variation->is_visible() && CatalogVisibility::SEARCH !== $variation->get_catalog_visibility() ) ||
+ ! $item->is_publicly_viewable() ||
+ ! $item->is_purchasable() ||
+ ! $item->is_in_stock() ||
+ ! $item->has_enough_stock( 1 )
+ ) {
+ return null;
+ }
+
+ if ( $variation && in_array( '', wc_get_product_variation_attributes( $variation->get_id() ), true ) ) {
+ return null;
+ }
+
+ return add_query_arg(
+ array(
+ 'checkout-link' => '1',
+ 'products' => rawurlencode( $item->get_id() . ':1' ),
+ ),
+ home_url( '/' )
+ );
+ }
+
/**
* Count how many of a variable product's variations match the given attribute selection.
*
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 93f2c8ad5e7..1de634d5454 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
@@ -1,6 +1,7 @@
<?php
declare( strict_types = 1 );
+use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Internal\ProductGallery\ProductMediaGallery;
/**
@@ -1082,6 +1083,87 @@ class WC_Structured_Data_Test extends \WC_Unit_Test_Case {
$product->delete( true );
}
+ /**
+ * @testdox A simple product's structured data links to checkout and respects the offer filter.
+ */
+ public function test_simple_product_schema_checkout_link(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ $this->structured_data->generate_product_data( $product );
+ $markup = $this->structured_data->get_data()[0];
+ $url = $markup['offers'][0]['checkoutPageURLTemplate'];
+
+ $this->assertSame( $url, $markup['potentialAction']['target']['urlTemplate'] );
+ wp_parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
+ $this->assertSame(
+ array(
+ 'checkout-link' => '1',
+ 'products' => $product->get_id() . ':1',
+ ),
+ $query
+ );
+
+ add_filter(
+ 'woocommerce_structured_data_product_offer',
+ static function ( $offer ) {
+ unset( $offer['checkoutPageURLTemplate'] );
+ return $offer;
+ }
+ );
+ $sut = new WC_Structured_Data();
+ $sut->generate_product_data( $product );
+ $markup = $sut->get_data()[0];
+ $this->assertArrayNotHasKey( 'checkoutPageURLTemplate', $markup['offers'][0] );
+ $this->assertArrayNotHasKey( 'potentialAction', $markup );
+ }
+
+ /**
+ * @testdox Only a fixed variation gets a checkout link in structured data.
+ * @testWith ["", false]
+ * ["Large", true]
+ *
+ * @param string $variation_size Variation size, or an empty string for any size.
+ * @param bool $has_link Whether a checkout link is expected.
+ */
+ public function test_variation_schema_checkout_link( string $variation_size, bool $has_link ): void {
+ $attribute = new WC_Product_Attribute();
+ $attribute->set_name( 'Size' );
+ $attribute->set_options( array( 'Large' ) );
+ $attribute->set_variation( true );
+
+ $product = new WC_Product_Variable();
+ $product->set_name( 'Shirt' );
+ $product->set_status( ProductStatus::PUBLISH );
+ $product->set_attributes( array( $attribute ) );
+ $product->save();
+
+ $variation = new WC_Product_Variation();
+ $variation->set_parent_id( $product->get_id() );
+ $variation->set_attributes( array( 'size' => $variation_size ) );
+ $variation->set_regular_price( '20' );
+ $variation->set_status( ProductStatus::PUBLISH );
+ $variation->save();
+ WC_Product_Variable::sync( $product->get_id() );
+ $product = wc_get_product( $product->get_id() );
+
+ $previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+ try {
+ $_GET = array( 'attribute_size' => 'Large' );
+ $this->structured_data->generate_product_data( $product );
+ $markup = $this->structured_data->get_data()[0];
+ if ( ! $has_link ) {
+ $this->assertArrayNotHasKey( 'checkoutPageURLTemplate', $markup['offers'][0] );
+ $this->assertArrayNotHasKey( 'potentialAction', $markup );
+ return;
+ }
+ $url = $markup['offers'][0]['checkoutPageURLTemplate'];
+ $this->assertSame( $url, $markup['potentialAction']['target']['urlTemplate'] );
+ wp_parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
+ $this->assertSame( $variation->get_id() . ':1', $query['products'] );
+ } finally {
+ $_GET = $previous_get;
+ }
+ }
+
/**
* When a variable product page is requested for a single, fully-specified variation, the offer
* should describe that variation with a single Offer and exact price (no AggregateOffer range),
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
index 8d0007468c2..0a129fbb12b 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
@@ -11,6 +11,48 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
* Unit tests for CheckoutLink.
*/
class CheckoutLinkTest extends \WC_Unit_Test_Case {
+ /**
+ * @testdox A generated checkout link adds the selected variation to the cart.
+ */
+ public function test_generated_variation_checkout_link_adds_to_cart(): void {
+ $attribute = new \WC_Product_Attribute();
+ $attribute->set_name( 'Size' );
+ $attribute->set_options( [ 'Large' ] );
+ $attribute->set_variation( true );
+
+ $product = new \WC_Product_Variable();
+ $product->set_name( 'Shirt' );
+ $product->set_status( ProductStatus::PUBLISH );
+ $product->set_attributes( [ $attribute ] );
+ $product->save();
+
+ $variation = new \WC_Product_Variation();
+ $variation->set_parent_id( $product->get_id() );
+ $variation->set_attributes( [ 'size' => 'Large' ] );
+ $variation->set_regular_price( '20' );
+ $variation->set_status( ProductStatus::PUBLISH );
+ $variation->save();
+ \WC_Product_Variable::sync( $product->get_id() );
+ $product = wc_get_product( $product->get_id() );
+
+ $previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+ try {
+ $_GET = [ 'attribute_size' => 'Large' ];
+ $sut = new \WC_Structured_Data();
+ $sut->generate_product_data( $product );
+ $url = $sut->get_data()[0]['offers'][0]['checkoutPageURLTemplate'];
+ wp_parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
+ $_GET = wp_slash( $query );
+ $this->get_checkout_link_service()->get_checkout_link_test();
+ $items = array_values( WC()->cart->get_cart() );
+ $this->assertCount( 1, $items );
+ $this->assertSame( $variation->get_id(), $items[0]['variation_id'] );
+ $this->assertSame( [ 'attribute_size' => 'Large' ], $items[0]['variation'] );
+ } finally {
+ $_GET = $previous_get;
+ }
+ }
+
/**
* @testdox Installing-mode requests queue the endpoint rewrite without replacing persisted rules.
*/