From 6eb9d6a83d82eed889fdb7edb0cb51f24c41f911 Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:50:13 +0100 Subject: [PATCH] Fix product count display on mini cart first render (#52124) Co-authored-by: Alex Florisca --- .../changelog/fix-mini-cart-button-setting | 4 + .../src/Blocks/BlockTypes/MiniCart.php | 9 +- .../php/src/Blocks/BlockTypes/MiniCart.php | 88 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-mini-cart-button-setting create mode 100644 plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php diff --git a/plugins/woocommerce/changelog/fix-mini-cart-button-setting b/plugins/woocommerce/changelog/fix-mini-cart-button-setting new file mode 100644 index 00000000000..7699e69a488 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-mini-cart-button-setting @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Set server-side render of Mini-Cart icon to correctly show/hide the count based on block setting diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php b/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php index 44cc0431f31..453526516f2 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php @@ -422,10 +422,17 @@ class MiniCart extends AbstractBlock { $product_count_color = array_key_exists( 'productCountColor', $attributes ) ? esc_attr( $attributes['productCountColor']['color'] ) : ''; $icon = MiniCartUtils::get_svg_icon( $attributes['miniCartIcon'] ?? '', $icon_color ); + $product_count_visibility = isset( $attributes['productCountVisibility'] ) ? $attributes['productCountVisibility'] : 'greater_than_zero'; + $show_product_count = 'always' === $product_count_visibility; + if ( 'greater_than_zero' === $product_count_visibility ) { + $cart = $this->get_cart_instance(); + $show_product_count = $cart && $cart->get_cart_contents_count() > 0; + } + $button_html = $this->get_cart_price_markup( $attributes ) . ' ' . $icon . ' - + ' . ( $show_product_count ? '' : '' ) . ' '; if ( is_cart() || is_checkout() ) { diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php new file mode 100644 index 00000000000..b1c28678aec --- /dev/null +++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/MiniCart.php @@ -0,0 +1,88 @@ +products = array( + $fixtures->get_simple_product( + array( + 'name' => 'Test Product 1', + 'stock_status' => 'instock', + 'regular_price' => 10, + 'weight' => 10, + ) + ), + ); + wc_empty_cart(); + add_filter( 'woocommerce_is_rest_api_request', '__return_false', 1 ); + } + + /** + * Tear down test. Called after every test. + * @return void + */ + protected function tearDown(): void { + parent::tearDown(); + remove_filter( 'woocommerce_is_rest_api_request', '__return_false', 1 ); + } + + /** + * Checks the output of the MiniCart block is correct based on the productCountVisibility attribute when cart is empty. + * @return void + */ + public function test_product_count_visibility_with_empty_cart() { + + // Test badge is shown when "always" is selected. + $block = parse_blocks( '' ); + $output = render_block( $block[0] ); + $this->assertStringContainsString( '' ); + $output = render_block( $block[0] ); + $this->assertStringNotContainsString( '' ); + $output = render_block( $block[0] ); + $this->assertStringNotContainsString( 'cart->add_to_cart( $this->products[0]->get_id(), 2 ); + + // Tests badge is shown with items in cart when "always" is selected. + $block = parse_blocks( '' ); + $output = render_block( $block[0] ); + $this->assertStringContainsString( '' ); + $output = render_block( $block[0] ); + $this->assertStringContainsString( '' ); + $output = render_block( $block[0] ); + $this->assertStringNotContainsString( '