Commit 61795c85b4f for woocommerce
commit 61795c85b4f830015c412d0e04e7591692d3852a
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 19:15:11 2026 +0100
Hide unpublished product names and attributes in add-to-cart errors (#71239)
Do not name unpublished products, or their attributes, in add to cart errors
(cherry picked from commit ecc96b677747f584612cff1eaf154c938a912d86)
Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index e38a2d746eb..63a8507ed9e 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -1225,6 +1225,15 @@ class WC_Cart extends WC_Legacy_Cart {
return false;
}
+ if ( ! $product_data->is_viewable() && ! $product_data->is_purchasable() ) {
+ /**
+ * This filter is documented further down in this method.
+ *
+ * @since 3.8.0
+ */
+ throw new Exception( apply_filters( 'woocommerce_cart_product_cannot_be_purchased_message', __( 'Sorry, this product cannot be purchased.', 'woocommerce' ), $product_data ) );
+ }
+
// Variable product cannot be added to cart without a specified variation.
if ( ! $variation_id && $product_data->is_type( ProductType::VARIABLE ) ) {
/* translators: 1: product link, 2: product name */
@@ -1341,7 +1350,7 @@ class WC_Cart extends WC_Legacy_Cart {
)
) {
$product = wc_get_product( $product_id );
- if ( ! ( $product instanceof WC_Product ) ) {
+ if ( ! ( $product instanceof WC_Product ) || ! $product->is_viewable() ) {
throw new Exception( __( 'The selected product is invalid.', 'woocommerce' ) );
}
diff --git a/plugins/woocommerce/includes/class-wc-form-handler.php b/plugins/woocommerce/includes/class-wc-form-handler.php
index ac91636bb0c..b732f7748d4 100644
--- a/plugins/woocommerce/includes/class-wc-form-handler.php
+++ b/plugins/woocommerce/includes/class-wc-form-handler.php
@@ -1474,7 +1474,7 @@ class WC_Form_Handler {
}
// Prevent parent variable product from being added to cart.
- if ( empty( $variation_id ) && $product && $product->is_type( ProductType::VARIABLE ) ) {
+ if ( empty( $variation_id ) && $product && $product->is_type( ProductType::VARIABLE ) && $product->is_viewable() ) {
$current_url = isset( $_SERVER['REQUEST_URI'] ) ? wp_parse_url( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), PHP_URL_PATH ) : '';
$product_url = wp_parse_url( get_permalink( $product_id ), PHP_URL_PATH );
$is_in_product_page = $current_url && $product_url && untrailingslashit( $current_url ) === untrailingslashit( $product_url );
diff --git a/plugins/woocommerce/src/StoreApi/Utilities/CartController.php b/plugins/woocommerce/src/StoreApi/Utilities/CartController.php
index dc4308fae37..0432a41db41 100644
--- a/plugins/woocommerce/src/StoreApi/Utilities/CartController.php
+++ b/plugins/woocommerce/src/StoreApi/Utilities/CartController.php
@@ -1156,6 +1156,11 @@ class CartController {
);
}
+ // Later errors name the product or its attributes, so refuse unpublished products the shopper cannot view before reaching them.
+ if ( ! $product->is_viewable() && ! $product->is_purchasable() ) {
+ $this->throw_default_product_exception( $product );
+ }
+
return $product;
}
@@ -1180,13 +1185,13 @@ class CartController {
}
/**
- * Get product name, hiding it for draft and private products.
+ * Get product name, hiding it for products the current user cannot view (unpublished products, or variations of one).
*
* @param \WC_Product $product Product instance.
* @return string
*/
protected function get_product_name( \WC_Product $product ) {
- if ( $product->get_status() === ProductStatus::DRAFT || $product->get_status() === ProductStatus::PRIVATE ) {
+ if ( ! $product->is_viewable() ) {
return '';
}
return $product->get_name();
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index 8a225387eeb..807a4b14754 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -941,6 +941,82 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
$variable_product->delete( true );
}
+ /**
+ * Create a variable product with an "Any" variation for the unpublished product tests.
+ *
+ * @param string $status Status of the variable product.
+ * @return WC_Product_Variation The (published) variation.
+ */
+ private function create_any_variation_of_product_with_status( string $status ): WC_Product_Variation {
+ $product = new WC_Product_Variable();
+ $attribute = new WC_Product_Attribute();
+ $attribute->set_name( 'Secret Finish' );
+ $attribute->set_options( array( 'Matte', 'Gloss' ) );
+ $attribute->set_variation( true );
+ $product->set_name( 'Unreleased Product' );
+ $product->set_status( $status );
+ $product->set_date_created( 'future' === $status ? time() + DAY_IN_SECONDS : time() );
+ $product->set_attributes( array( $attribute ) );
+ $product->save();
+
+ $variation = new WC_Product_Variation();
+ $variation->set_parent_id( $product->get_id() );
+ $variation->set_attributes( array( 'secret-finish' => '' ) );
+ $variation->set_regular_price( '10' );
+ $variation->save();
+
+ return $variation;
+ }
+
+ /**
+ * @testdox Add to cart errors do not name a variable product, or its attributes, when the shopper cannot view it.
+ * @testWith ["draft"]
+ * ["pending"]
+ * ["private"]
+ * ["future"]
+ * @param string $status Status of the variable product.
+ */
+ public function test_add_to_cart_errors_do_not_name_products_the_shopper_cannot_view( string $status ): void {
+ wp_set_current_user( 0 );
+ WC()->cart->empty_cart();
+
+ $variation = $this->create_any_variation_of_product_with_status( $status );
+ $product_id = $variation->get_parent_id();
+ $single_product = WC_Helper_Product::create_simple_product();
+
+ $requests = array(
+ 'no variation' => array( $product_id, 1, 0, array(), 'Sorry, this product cannot be purchased.' ),
+ 'missing attribute' => array( $product_id, 1, $variation->get_id(), array(), 'Sorry, this product cannot be purchased.' ),
+ 'invalid attribute' => array( $product_id, 1, $variation->get_id(), array( 'attribute_secret-finish' => 'Nope' ), 'Sorry, this product cannot be purchased.' ),
+ 'variation by ID' => array( $variation->get_id(), 1, 0, array(), 'Sorry, this product cannot be purchased.' ),
+ 'foreign variation' => array( $product_id, 1, $single_product->get_id(), array(), 'The selected product is invalid.' ),
+ );
+
+ foreach ( $requests as $label => $request ) {
+ WC()->session->set( 'wc_notices', null );
+ $expected = array_pop( $request );
+
+ $this->assertFalse( WC()->cart->add_to_cart( ...$request ), $label );
+ $this->assertSame( array( $expected ), wp_list_pluck( wc_get_notices( 'error' ), 'notice' ), $label );
+ }
+
+ $this->assertCount( 0, WC()->cart->get_cart_contents() );
+ }
+
+ /**
+ * @testdox Add to cart errors still name an unpublished variable product for a user who can edit it.
+ */
+ public function test_add_to_cart_errors_name_unpublished_products_the_user_can_edit(): void {
+ wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
+ WC()->cart->empty_cart();
+ WC()->session->set( 'wc_notices', null );
+
+ $variation = $this->create_any_variation_of_product_with_status( 'draft' );
+
+ $this->assertFalse( WC()->cart->add_to_cart( $variation->get_parent_id() ) );
+ $this->assertStringContainsString( 'Unreleased Product', wc_get_notices( 'error' )[0]['notice'] );
+ }
+
/**
* Test cloning cart holds no references in session
*/
@@ -1652,6 +1728,24 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
$this->assertMatchesRegularExpression( '/Please choose product options by visiting/', $notices['error'][0]['notice'] );
}
+ /**
+ * @testdox The add to cart form handler does not name a variable product the shopper cannot view.
+ *
+ * @see WC_Form_Handler::add_to_cart_action()
+ */
+ public function test_form_handler_add_to_cart_action_with_unpublished_parent_variable_product(): void {
+ wp_set_current_user( 0 );
+ WC()->session->set( 'wc_notices', null );
+
+ $_REQUEST['add-to-cart'] = $this->create_any_variation_of_product_with_status( 'draft' )->get_parent_id();
+
+ WC_Form_Handler::add_to_cart_action();
+
+ unset( $_REQUEST['add-to-cart'] );
+
+ $this->assertSame( array( 'Sorry, this product cannot be purchased.' ), wp_list_pluck( wc_get_notices( 'error' ), 'notice' ) );
+ }
+
/**
* Test case sensitivity fix for coupon discount amounts.
*
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 69704350197..6a240922091 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Domain/Services/CheckoutLinkTest.php
@@ -296,7 +296,7 @@ class CheckoutLinkTest extends \WC_Unit_Test_Case {
wp_parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
$this->assertStringNotContainsString( 'attribute_pa_size', $url, 'An unpublished product page should not be used as the redirect.' );
- $this->assertStringContainsString( 'Missing variation data', $query['wc_error'] ?? '', 'The add to cart error should still be shown.' );
+ $this->assertSame( 'This item is not available for purchase.', $query['wc_error'], 'The add to cart error should not name the unpublished product or its attributes.' );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Cart.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Cart.php
index f2929cfeb28..540e16a6b10 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Cart.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Cart.php
@@ -1134,6 +1134,73 @@ class Cart extends ControllerTestCase {
);
}
+ /**
+ * @testdox Add item errors do not name a variable product, or its attributes, when the shopper cannot view it.
+ * @testWith ["draft"]
+ * ["pending"]
+ * ["private"]
+ * ["future"]
+ * @param string $status Status of the variable product.
+ */
+ public function test_add_item_errors_do_not_name_products_the_shopper_cannot_view( string $status ) {
+ wp_set_current_user( 0 );
+ wc_empty_cart();
+
+ $fixtures = new FixtureData();
+
+ $variable_product = $fixtures->get_variable_product(
+ array(
+ 'name' => 'Unreleased Product',
+ 'status' => $status,
+ 'date_created' => 'future' === $status ? time() + DAY_IN_SECONDS : time(),
+ 'stock_status' => ProductStockStatus::IN_STOCK,
+ 'regular_price' => 10,
+ ),
+ array(
+ array(
+ 'attribute_id' => 0,
+ 'attribute_taxonomy' => 'Secret Finish',
+ 'term_ids' => array( 'Matte', 'Gloss' ),
+ ),
+ )
+ );
+
+ // The variation itself stays published and accepts any value.
+ $variation = $fixtures->get_variation_product( $variable_product->get_id(), array( 'secret-finish' => '' ) );
+
+ $requests = array(
+ 'no variation' => array( $variable_product->get_id(), array() ),
+ 'missing attribute' => array( $variation->get_id(), array() ),
+ 'invalid attribute' => array( $variation->get_id(), array( 'Secret Finish' => 'Nope' ) ),
+ 'valid attribute' => array( $variation->get_id(), array( 'Secret Finish' => 'Gloss' ) ),
+ );
+
+ foreach ( $requests as $label => list( $id, $attributes ) ) {
+ $request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/add-item' );
+ $request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
+ $request->set_body_params(
+ array(
+ 'id' => $id,
+ 'quantity' => 1,
+ 'variation' => array_map(
+ fn( $attribute, $value ) => array(
+ 'attribute' => $attribute,
+ 'value' => $value,
+ ),
+ array_keys( $attributes ),
+ $attributes
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertSame( 400, $response->get_status(), $label );
+ $this->assertSame( 'woocommerce_rest_product_not_purchasable', $response->get_data()['code'], $label );
+ $this->assertSame( 'This item is not available for purchase.', $response->get_data()['message'], $label );
+ }
+ }
+
/**
* Test adding item to cart with quantity 0 shows an error.
*/