Commit ba879510a37 for woocommerce
commit ba879510a37e394cb4f94753e2a88cd66970638f
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 17:12:45 2026 +0100
Keep checkout links from bypassing the sold-individually limit (#71209)
* Keep checkout links from bypassing the sold-individually limit
(cherry picked from commit 4fb5cb4cac13f90eae9e7f8c07a5d4425c72dc2f)
* Carry over the original checkout URL documentation
* Set the checkout link filter @since to 11.3.1
---------
Co-authored-by: Jorge Torres <jorge.torres@automattic.com>
diff --git a/docs/best-practices/urls-and-routing/checkout-urls.md b/docs/best-practices/urls-and-routing/checkout-urls.md
index 4071995d6af..b70d1392007 100644
--- a/docs/best-practices/urls-and-routing/checkout-urls.md
+++ b/docs/best-practices/urls-and-routing/checkout-urls.md
@@ -29,6 +29,12 @@ The product identifier can be:
Variation and cart item data use `key=value` pairs. Separate multiple pairs in the same field with semicolons. Variation data selects a variation, while cart item data passes additional values consumed by extensions or custom product types. The accepted cart item data depends on the extension handling the product.
+Cart item data in a checkout link comes from the shopper, not a trusted extension. Treat it as untrusted input, the same as any other external data.
+
+:::note
+For products sold individually, cart item data is dropped by default: since the shopper is in charge of this input, letting it vary would let a checkout link add more than one unit. An extension can restore it for a specific product with the `woocommerce_checkout_link_sold_individually_cart_item_data` filter, and restored data then behaves like any other item data. To keep both the data and the limit, add a check to the `woocommerce_add_cart_item_data` filter (it runs on every add to cart) that rejects the add if the cart already has a line for that product.
+:::
+
For example:
```plaintext
diff --git a/plugins/woocommerce/client/blocks/docs/third-party-developers/extensibility/hooks/filters.md b/plugins/woocommerce/client/blocks/docs/third-party-developers/extensibility/hooks/filters.md
index 838d354ab91..47385ffdee4 100644
--- a/plugins/woocommerce/client/blocks/docs/third-party-developers/extensibility/hooks/filters.md
+++ b/plugins/woocommerce/client/blocks/docs/third-party-developers/extensibility/hooks/filters.md
@@ -35,6 +35,7 @@
- [woocommerce_breadcrumb_product_terms_args](#woocommerce_breadcrumb_product_terms_args)
- [woocommerce_cart_contents_changed](#woocommerce_cart_contents_changed)
- [woocommerce_cart_item_permalink](#woocommerce_cart_item_permalink)
+- [woocommerce_checkout_link_sold_individually_cart_item_data](#woocommerce_checkout_link_sold_individually_cart_item_data)
- [woocommerce_checkout_must_be_logged_in_message](#woocommerce_checkout_must_be_logged_in_message)
- [woocommerce_checkout_show_terms](#woocommerce_checkout_show_terms)
- [woocommerce_default_payment_method](#woocommerce_default_payment_method)
@@ -866,6 +867,33 @@ This is a hook taken from the legacy cart/mini-cart templates that allows the pe
---
+## woocommerce_checkout_link_sold_individually_cart_item_data
+
+
+Filters the cart item data kept for a sold-individually product or variation added via a checkout link.
+
+```php
+apply_filters( 'woocommerce_checkout_link_sold_individually_cart_item_data', array $cart_item_data, array $raw_cart_item_data, \WC_Product $product )
+```
+
+### Description
+
+Empty by default, so repeated adds of the same product share one cart item and the one-per-order limit holds. Returning data lets it vary, so the limit is no longer guaranteed.
+
+### Parameters
+
+| Argument | Type | Description |
+| -------- | ---- | ----------- |
+| $cart_item_data | array | Cart item data to keep. Empty by default. |
+| $raw_cart_item_data | array | Cart item data parsed from the link, before stripping. |
+| $product | \WC_Product | The resolved product or variation. |
+
+### Source
+
+- [Blocks/Domain/Services/CheckoutLink.php](../../../../../../src/Blocks/Domain/Services/CheckoutLink.php)
+
+---
+
## woocommerce_checkout_must_be_logged_in_message
diff --git a/plugins/woocommerce/src/Blocks/Domain/Services/CheckoutLink.php b/plugins/woocommerce/src/Blocks/Domain/Services/CheckoutLink.php
index 7042f1f1123..81ab5a9507a 100644
--- a/plugins/woocommerce/src/Blocks/Domain/Services/CheckoutLink.php
+++ b/plugins/woocommerce/src/Blocks/Domain/Services/CheckoutLink.php
@@ -130,6 +130,24 @@ class CheckoutLink {
continue;
}
+ $product = wc_get_product( $product_id );
+
+ if ( $product && $product->is_sold_individually() ) {
+ /**
+ * Filters the cart item data kept for a sold-individually product or variation added via a checkout link.
+ *
+ * Empty by default, so repeated adds of the same product share one cart item and the one-per-order
+ * limit holds. Returning data lets it vary, so the limit is no longer guaranteed.
+ *
+ * @since 11.3.1
+ *
+ * @param array $cart_item_data Cart item data to keep. Empty by default.
+ * @param array $raw_cart_item_data Cart item data parsed from the link, before stripping.
+ * @param \WC_Product $product The resolved product or variation.
+ */
+ $cart_item_data = (array) apply_filters( 'woocommerce_checkout_link_sold_individually_cart_item_data', [], $cart_item_data, $product );
+ }
+
$variation = array_map(
function ( $key, $value ) {
return [
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 0a129fbb12b..69704350197 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
@@ -299,6 +299,58 @@ class CheckoutLinkTest extends \WC_Unit_Test_Case {
$this->assertStringContainsString( 'Missing variation data', $query['wc_error'] ?? '', 'The add to cart error should still be shown.' );
}
+ /**
+ * @testdox Strips cart item data for sold-individually products and variations by default, restores it only where the override filter allows, and still blocks a later plain add.
+ */
+ public function test_cart_item_data_does_not_bypass_sold_individually(): void {
+ $default_product = \WC_Helper_Product::create_simple_product();
+ $default_product->set_sold_individually( true );
+ $default_product->save();
+
+ $overridden_product = \WC_Helper_Product::create_simple_product();
+ $overridden_product->set_sold_individually( true );
+ $overridden_product->save();
+
+ $variable_product = \WC_Helper_Product::create_variation_product();
+ $variable_product->set_sold_individually( true );
+ $variable_product->save();
+
+ $variation_id = wc_get_product_id_by_sku( 'DUMMY SKU VARIABLE SMALL' );
+
+ add_filter(
+ 'woocommerce_checkout_link_sold_individually_cart_item_data',
+ function ( $cart_item_data, $raw_cart_item_data, $product ) use ( $overridden_product ) {
+ return $product->get_id() === $overridden_product->get_id() ? $raw_cart_item_data : $cart_item_data;
+ },
+ 10,
+ 3
+ );
+
+ $_GET['products'] = implode(
+ ',',
+ [
+ $default_product->get_id() . ':1::note=a',
+ $default_product->get_id() . ':1::note=b',
+ $overridden_product->get_id() . ':1::nyp=120',
+ $variation_id . ':1:pa_colour=red;pa_number=0:note=a',
+ ]
+ );
+
+ $this->get_checkout_link_service()->get_checkout_link_test();
+
+ $cart_items = array_column( WC()->cart->get_cart(), null, 'product_id' );
+
+ $this->assertCount( 3, $cart_items, 'Each sold-individually product and variation should only be added to the cart once.' );
+ $this->assertSame( 1, $cart_items[ $default_product->get_id() ]['quantity'], 'A sold-individually product should keep a quantity of one.' );
+ $this->assertArrayNotHasKey( 'note', $cart_items[ $default_product->get_id() ], 'Cart item data should be dropped by default, even across several entries.' );
+ $this->assertSame( WC()->cart->generate_cart_id( $default_product->get_id() ), $cart_items[ $default_product->get_id() ]['key'], 'A sold-individually product should keep its plain cart item key.' );
+ $this->assertSame( '120', $cart_items[ $overridden_product->get_id() ]['nyp'] ?? null, 'The override filter should restore data only for the product it targets.' );
+ $this->assertArrayNotHasKey( 'note', $cart_items[ $variable_product->get_id() ], 'Cart item data should be dropped for a variation whose parent is sold individually.' );
+
+ $this->assertFalse( WC()->cart->add_to_cart( $default_product->get_id() ), 'A later plain add should not bypass the sold-individually limit either.' );
+ $this->assertCount( 3, WC()->cart->get_cart(), 'The cart should still only contain one line per product.' );
+ }
+
/**
* Get a checkout link service that exposes the checkout link for testing.
*