Fix product count display on mini cart first render (#52124)

Co-authored-by: Alex Florisca <alex.florisca@automattic.com>
This commit is contained in:
Thomas Roberts 2024-10-23 15:50:13 +01:00 committed by GitHub
parent 42c7d33dbb
commit 6eb9d6a83d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 100 additions and 1 deletions

View File

@ -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

View File

@ -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 ) . '
<span class="wc-block-mini-cart__quantity-badge">
' . $icon . '
<span class="wc-block-mini-cart__badge" style="background:' . $product_count_color . '"></span>
' . ( $show_product_count ? '<span class="wc-block-mini-cart__badge" style="background:' . $product_count_color . '"></span>' : '' ) . '
</span>';
if ( is_cart() || is_checkout() ) {

View File

@ -0,0 +1,88 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
/**
* Tests for the Checkout block type
*
* @since $VID:$
*/
class MiniCart extends \WP_UnitTestCase {
/**
* Setup test product data. Called before every test.
*/
public function setUp(): void {
parent::setUp();
$fixtures = new FixtureData();
$this->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( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"always"} /-->' );
$output = render_block( $block[0] );
$this->assertStringContainsString( '<span class="wc-block-mini-cart__badge"', $output );
// Tests badge is not shown, because product count is not greater than zero when "greater_than_zero" is selected.
$block = parse_blocks( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"greater_than_zero"} /-->' );
$output = render_block( $block[0] );
$this->assertStringNotContainsString( '<span class="wc-block-mini-cart__badge"', $output );
// Tests badge is not shown when "never" is selected.
$block = parse_blocks( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"never"} /-->' );
$output = render_block( $block[0] );
$this->assertStringNotContainsString( '<span class="wc-block-mini-cart__badge"', $output );
}
/**
* Checks the output of the MiniCart block is correct based on the productCountVisibility attribute when cart has products.
* @return void
*/
public function test_product_count_visibility_with_products_in_cart() {
WC()->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( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"always"} /-->' );
$output = render_block( $block[0] );
$this->assertStringContainsString( '<span class="wc-block-mini-cart__badge"', $output );
// Tests badge *is* shown, because product count is greater than zero when "greater_than_zero" is selected.
$block = parse_blocks( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"greater_than_zero"} /-->' );
$output = render_block( $block[0] );
$this->assertStringContainsString( '<span class="wc-block-mini-cart__badge"', $output );
// Tests badge is not shown with items in cart when "never" is selected.
$block = parse_blocks( '<!-- wp:woocommerce/mini-cart {"productCountVisibility":"never"} /-->' );
$output = render_block( $block[0] );
$this->assertStringNotContainsString( '<span class="wc-block-mini-cart__badge"', $output );
}
}