Automattic\WooCommerce\Blocks\BlockTypes

MiniCart::render_experimental_iapi_mini_cartprotectedWC 1.0

Render an experimental interactivity API powered Mini-Cart block.

Method of the class: MiniCart{}

No Hooks.

Returns

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render_experimental_iapi_mini_cart( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

MiniCart::render_experimental_iapi_mini_cart() code WC 10.7.0

<?php
protected function render_experimental_iapi_mini_cart( $attributes, $content, $block ) {
	wp_enqueue_script_module( $this->get_full_block_name() );

	// Enqueue all integration scripts registered for this block.
	$integration_script_handles = $this->integration_registry->get_all_registered_script_handles();
	foreach ( $integration_script_handles as $handle ) {
		wp_enqueue_script( $handle );
	}

	$consent = 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WooCommerce';
	BlocksSharedState::load_cart_state( $consent );
	BlocksSharedState::load_store_config( $consent );
	BlocksSharedState::load_placeholder_image( $consent );

	$cart = $this->get_cart_instance();

	if ( $cart ) {
		$classes_styles                   = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
		$icon_color                       = isset( $attributes['iconColor']['color'] ) ? esc_attr( $attributes['iconColor']['color'] ) : 'currentColor';
		$product_count_color              = isset( $attributes['productCountColor']['color'] ) ? esc_attr( $attributes['productCountColor']['color'] ) : '';
		$styles                           = $product_count_color ? 'background:' . $product_count_color : '';
		$icon                             = MiniCartUtils::get_svg_icon( $attributes['miniCartIcon'] ?? '', $icon_color );
		$product_count_visibility         = isset( $attributes['productCountVisibility'] ) ? $attributes['productCountVisibility'] : 'greater_than_zero';
		$wrapper_classes                  = sprintf( 'wc-block-mini-cart wp-block-woocommerce-mini-cart %s', $classes_styles['classes'] );
		$wrapper_styles                   = $classes_styles['styles'];
		$template_part_contents           = $this->get_template_part_contents( false );
		$template_part_contents           = do_blocks( $this->process_template_contents( $template_part_contents ) );
		$cart_item_count                  = $cart ? $cart->get_cart_contents_count() : 0;
		$display_cart_price_including_tax = get_option( 'woocommerce_tax_display_cart' ) === 'incl';
		$cart_item_count                  = $cart ? $cart->get_cart_contents_count() : 0;
		$badge_is_visible                 = ( 'always' === $product_count_visibility ) || ( 'never' !== $product_count_visibility && $cart_item_count > 0 );
		$formatted_subtotal               = '';
		$html                             = new \WP_HTML_Tag_Processor( wc_price( $cart->get_displayed_subtotal() ) );
		$on_cart_click_behaviour          = isset( $attributes['onCartClickBehaviour'] ) ? $attributes['onCartClickBehaviour'] : 'open_drawer';

		if ( $html->next_tag( 'bdi' ) ) {
			while ( $html->next_token() ) {
				if ( '#text' === $html->get_token_name() ) {
					$formatted_subtotal .= $html->get_modifiable_text();
				}
			}
		}

		// The following translation is a temporary workaround. It will be
		// reverted to the previous form (`%1$d item in cart`) as soon as the
		// `@wordpress/i18n` package is available as a script module.
		$button_aria_label_template = isset( $attributes['hasHiddenPrice'] ) && false !== $attributes['hasHiddenPrice']
			/* translators: %d is the number of products in the cart. */
			? __( 'Number of items in the cart: %d', 'woocommerce' )
			/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
			: __( 'Number of items in the cart: %1$d. Total price of %2$s', 'woocommerce' );

		wp_interactivity_state(
			$this->get_full_block_name(),
			array(
				'isOpen'             => false,
				'totalItemsInCart'   => $cart_item_count,
				'shouldShowTaxLabel' => $cart->get_cart_contents_tax() > 0,
				'badgeIsVisible'     => $badge_is_visible,
				'formattedSubtotal'  => $formatted_subtotal,
				'drawerOverlayClass' => 'wc-block-components-drawer__screen-overlay wc-block-components-drawer__screen-overlay--with-slide-out wc-block-components-drawer__screen-overlay--is-hidden',
				'buttonAriaLabel'    => function () use ( $button_aria_label_template ) {
					$state = wp_interactivity_state();
					return isset( $attributes['hasHiddenPrice'] ) && false !== $attributes['hasHiddenPrice']
						? sprintf( $button_aria_label_template, $state['totalItemsInCart'] )
						: sprintf( $button_aria_label_template, $state['totalItemsInCart'], $state['formattedSubtotal'] );
				},
			)
		);

		$context = array(
			'productCountVisibility' => $product_count_visibility,
		);

		wp_interactivity_config(
			$this->get_full_block_name(),
			array(
				'displayCartPriceIncludingTax' => $display_cart_price_including_tax,
				'onCartClickBehaviour'         => $on_cart_click_behaviour,
				'checkoutUrl'                  => wc_get_checkout_url(),
				'buttonAriaLabelTemplate'      => $button_aria_label_template,
			)
		);

		$cart_always_shows_price = isset( $attributes['hasHiddenPrice'] ) && false === $attributes['hasHiddenPrice'];
		$price_color             = isset( $attributes['priceColor']['color'] ) ? $attributes['priceColor']['color'] : '';

		$button_role = 'navigate_to_checkout' === $on_cart_click_behaviour
			? 'role="link"'
			: '';

		// Render the minicart overlay in the body, outside of the block itself.
		if ( ! has_action( 'wp_footer', array( $this, 'render_experimental_iapi_mini_cart_overlay' ) ) ) {
			add_action( 'wp_footer', array( $this, 'render_experimental_iapi_mini_cart_overlay' ) );
		}
		ob_start();
		?>
	
		<div
			data-wp-interactive="woocommerce/mini-cart"
			data-wp-init="callbacks.setupJQueryEventBridge"
			data-wp-on-document--wc-blocks_added_to_cart="woocommerce::actions.refreshCartItems"
			data-wp-on-document--wc-blocks_removed_from_cart="woocommerce::actions.refreshCartItems"
			<?php if ( 'open_drawer' === $attributes['addToCartBehaviour'] ) : ?>
			data-wp-on-document--wc-blocks_added_to_cart---open-drawer="actions.openDrawer"
			<?php endif; ?>
			data-wp-watch="callbacks.disableScrollingOnBody"
			data-wp-init--mark-as-hydrated="callbacks.markAsHydrated"
			<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			<?php echo wp_interactivity_data_wp_context( $context ); ?>
			class="<?php echo esc_attr( $wrapper_classes ); ?>"
			style="<?php echo esc_attr( $wrapper_styles ); ?>"
		>
			<button
				data-wp-on--click="actions.openDrawer"
				data-wp-bind--aria-label="state.buttonAriaLabel"
				class="wc-block-mini-cart__button"
				<?php echo $button_role; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			>
				<span class="wc-block-mini-cart__quantity-badge">
					<?php
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						echo $icon;
					?>
						<?php if ( 'never' !== $product_count_visibility ) : ?>
							<span
								data-wp-style--background-color="state.badgeBackgroundColor"
								data-wp-style--color="state.badgeTextColor"
								data-wp-bind--hidden="!state.badgeIsVisible"
								data-wp-text="state.totalItemsInCart"
								class="wc-block-mini-cart__badge"
								style="<?php echo esc_attr( $styles ); ?>"
							>
						</span>
					<?php endif; ?>
				</span>
				<?php if ( $cart_always_shows_price ) : ?>
					<span data-wp-text="state.formattedSubtotal" class="wc-block-mini-cart__amount" style="<?php echo 'color:' . esc_attr( $price_color ); ?>">
					</span>
					<?php if ( ! empty( $this->tax_label ) ) : ?>
						<small
							data-wp-bind--hidden="!state.shouldShowTaxLabel"
							class="wc-block-mini-cart__tax-label"
							style="color:<?php echo esc_attr( $price_color ); ?>"
						>
							<?php echo esc_html( $this->tax_label ); ?>
						</small>
					<?php endif; ?>
				<?php endif; ?>
			</button>
		</div>
		<?php
		return ob_get_clean();
	}

	return '';
}