Commit 9d2b6f6b60 for woocommerce
commit 9d2b6f6b60f243ac44d4bde2fe701b9cbd8fb7f2
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Apr 25 10:11:35 2025 +0200
Hide 'View Cart' button when the cart page is not defined (#57495)
* Hide 'View Cart' button when the cart page is not defined
* Add changefile(s) from automation for the following project(s): woocommerce
* add unit test
* fix unit test
---------
Co-authored-by: github-actions <github-actions@github.com>
diff --git a/plugins/woocommerce/changelog/57495-wooplug-3535-cart-link-shown-when-adding-to-the-cart-on-store-with-no b/plugins/woocommerce/changelog/57495-wooplug-3535-cart-link-shown-when-adding-to-the-cart-on-store-with-no
new file mode 100644
index 0000000000..09fd7af204
--- /dev/null
+++ b/plugins/woocommerce/changelog/57495-wooplug-3535-cart-link-shown-when-adding-to-the-cart-on-store-with-no
@@ -0,0 +1,4 @@
+Significance: minor
+Type: fix
+
+Hide 'View Cart' button when the cart page is not defined.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index fbdb6c11b4..2a521a3bcd 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -9,6 +9,7 @@
* @version 2.1.0
*/
+use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\Utilities\DiscountsUtil;
@@ -1193,7 +1194,13 @@ class WC_Cart extends WC_Legacy_Cart {
$message = apply_filters( 'woocommerce_cart_product_cannot_add_another_message', $message, $product_data );
$wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
- throw new Exception( sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', $message, esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), __( 'View cart', 'woocommerce' ) ) );
+ if ( ! CartCheckoutUtils::has_cart_page() ) {
+ $message = sprintf( '%s', esc_html( $message ) );
+ } else {
+ $message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', $message, esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), __( 'View cart', 'woocommerce' ) );
+ }
+
+ throw new Exception( $message );
}
}
@@ -1254,13 +1261,17 @@ class WC_Cart extends WC_Legacy_Cart {
$stock_quantity_in_cart = $products_qty_in_cart[ $product_data->get_stock_managed_by_id() ];
$wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
- $message = sprintf(
+ $message = CartCheckoutUtils::has_cart_page() ? sprintf(
'%s <a href="%s" class="button wc-forward%s">%s</a>',
/* translators: 1: quantity in stock 2: current quantity */
sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) ),
esc_url( wc_get_cart_url() ),
esc_attr( $wp_button_class ),
__( 'View cart', 'woocommerce' )
+ ) : sprintf(
+ '%s',
+ /* translators: 1: quantity in stock 2: current quantity */
+ sprintf( __( 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_quantity, $product_data ), wc_format_stock_quantity_for_display( $stock_quantity_in_cart, $product_data ) )
);
/**
diff --git a/plugins/woocommerce/includes/wc-cart-functions.php b/plugins/woocommerce/includes/wc-cart-functions.php
index c33f56e1e2..911f4c2215 100644
--- a/plugins/woocommerce/includes/wc-cart-functions.php
+++ b/plugins/woocommerce/includes/wc-cart-functions.php
@@ -9,6 +9,7 @@
*/
use Automattic\Jetpack\Constants;
+use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
@@ -101,6 +102,8 @@ function wc_add_to_cart_message( $products, $show_qty = false, $return = false )
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_attr( $wp_button_class ), esc_html__( 'Continue shopping', 'woocommerce' ) );
+ } elseif ( ! CartCheckoutUtils::has_cart_page() ) {
+ $message = sprintf( '%s', esc_html( $added_text ) );
} else {
$message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), esc_html__( 'View cart', 'woocommerce' ) );
}
diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php
index b207646822..62c640015c 100644
--- a/plugins/woocommerce/includes/wc-template-functions.php
+++ b/plugins/woocommerce/includes/wc-template-functions.php
@@ -9,6 +9,7 @@
*/
use Automattic\Jetpack\Constants;
+use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\Internal\Utilities\HtmlSanitizer;
@@ -2468,6 +2469,9 @@ if ( ! function_exists( 'woocommerce_widget_shopping_cart_button_view_cart' ) )
*/
function woocommerce_widget_shopping_cart_button_view_cart() {
$wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
+ if ( ! CartCheckoutUtils::has_cart_page() ) {
+ echo '';
+ }
echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward' . esc_attr( $wp_button_class ) . '">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>';
}
}
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
index ec21c17207..06744e6c94 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
@@ -3,6 +3,7 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Blocks\BlockTypes;
+use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
use Automattic\WooCommerce\Enums\ProductType;
@@ -275,7 +276,7 @@ class ProductButton extends AbstractBlock {
'{div_directives}' => $is_ajax_button ? $div_directives : '',
'{button_directives}' => $is_ajax_button ? $button_directives : $anchor_directive,
'{span_button_directives}' => $is_ajax_button ? $span_button_directives : '',
- '{view_cart_html}' => $is_ajax_button ? $this->get_view_cart_html() : '',
+ '{view_cart_html}' => $is_ajax_button && CartCheckoutUtils::has_cart_page() ? $this->get_view_cart_html() : '',
)
),
$product,
diff --git a/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php b/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php
index da711dbabd..0b18474a03 100644
--- a/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php
+++ b/plugins/woocommerce/src/Blocks/Utils/CartCheckoutUtils.php
@@ -453,4 +453,13 @@ class CartCheckoutUtils {
}
}
}
+
+ /**
+ * Check if the cart page is defined.
+ *
+ * @return bool True if the cart page is defined, false otherwise.
+ */
+ public static function has_cart_page() {
+ return wc_get_page_permalink( 'cart', -1 ) !== -1;
+ }
}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/cart/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/cart/functions.php
index 742e6a34cc..ee4e07b1a5 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/cart/functions.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/cart/functions.php
@@ -134,28 +134,60 @@ class WC_Tests_Cart_Functions extends WC_Unit_Test_Case {
}
/**
- * Test wc_add_to_cart_message
+ * Test wc_add_to_cart_message with cart page defined.
*/
- public function test_wc_add_to_cart_message() {
- $product = WC_Helper_Product::create_simple_product();
+ public function test_wc_add_to_cart_message_with_cart_page_defined() {
+ $product = WC_Helper_Product::create_simple_product();
+
+ wc_create_page( 'cart', 'woocommerce_cart_page_id', 'Cart', '' );
+
+ $cart_page_url = wc_get_page_permalink( 'cart' );
+
$wp_button_class = esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' );
$message = wc_add_to_cart_message( array( $product->get_id() => 1 ), false, true );
- $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ $message = wc_add_to_cart_message( array( $product->get_id() => 3 ), false, true );
+ $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ $message = wc_add_to_cart_message( array( $product->get_id() => 1 ), true, true );
+ $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ $message = wc_add_to_cart_message( array( $product->get_id() => 3 ), true, true );
+ $this->assertEquals( '3 × “Dummy Product” have been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ $message = wc_add_to_cart_message( $product->get_id(), false, true );
+ $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ $message = wc_add_to_cart_message( $product->get_id(), true, true );
+ $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="' . $cart_page_url . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+
+ delete_option( 'woocommerce_cart_page_id' );
+ }
+
+ /**
+ * Test wc_add_to_cart_message with cart page not defined.
+ */
+ public function test_wc_add_to_cart_message_with_cart_page_not_defined() {
+ $product = WC_Helper_Product::create_simple_product();
+
+ $message = wc_add_to_cart_message( array( $product->get_id() => 1 ), false, true );
+ $this->assertEquals( '“Dummy Product” has been added to your cart.', $message );
$message = wc_add_to_cart_message( array( $product->get_id() => 3 ), false, true );
- $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '“Dummy Product” has been added to your cart.', $message );
$message = wc_add_to_cart_message( array( $product->get_id() => 1 ), true, true );
- $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '“Dummy Product” has been added to your cart.', $message );
$message = wc_add_to_cart_message( array( $product->get_id() => 3 ), true, true );
- $this->assertEquals( '3 × “Dummy Product” have been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '3 × “Dummy Product” have been added to your cart.', $message );
$message = wc_add_to_cart_message( $product->get_id(), false, true );
- $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '“Dummy Product” has been added to your cart.', $message );
$message = wc_add_to_cart_message( $product->get_id(), true, true );
- $this->assertEquals( '“Dummy Product” has been added to your cart. <a href="http://' . WP_TESTS_DOMAIN . '" class="button wc-forward' . $wp_button_class . '">View cart</a>', $message );
+ $this->assertEquals( '“Dummy Product” has been added to your cart.', $message );
}
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Utils/CartCheckoutUtilsTest.php b/plugins/woocommerce/tests/php/src/Blocks/Utils/CartCheckoutUtilsTest.php
index 121959b9da..b1e92b56f3 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Utils/CartCheckoutUtilsTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Utils/CartCheckoutUtilsTest.php
@@ -76,4 +76,14 @@ class CartCheckoutUtilsTest extends WP_UnitTestCase {
$this->assertEquals( 'required', get_option( 'woocommerce_checkout_company_field' ) );
$this->assertEquals( 'required', get_option( 'woocommerce_checkout_address_2_field' ) );
}
+
+ /**
+ * Test has_cart_page() function.
+ */
+ public function test_has_cart_page() {
+ wc_create_page( 'cart', 'woocommerce_cart_page_id', 'Cart', '' );
+ $this->assertTrue( CartCheckoutUtils::has_cart_page() );
+ delete_option( 'woocommerce_cart_page_id' );
+ $this->assertFalse( CartCheckoutUtils::has_cart_page() );
+ }
}