Automattic\WooCommerce\Blocks\BlockTypes

Wishlist::renderprotectedWC 1.0

Render the block.

Method of the class: Wishlist{}

No Hooks.

Returns

String. Rendered block type output.

Usage

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

Wishlist::render() code WC 10.9.1

protected function render( $attributes, $content, $block ) {
	// Guests have no personal list — bail before enqueuing assets or
	// seeding state. The My Account endpoint isn't reachable for
	// guests, but the block can also be placed by a merchant on any
	// page, where this guard is what stops it from rendering an
	// empty shell for logged-out visitors.
	if ( ! is_user_logged_in() ) {
		return '';
	}

	// Clamp to the 2-6 range the SCSS `@for $i from 2 through 6` loop
	// and the editor `RangeControl` both support. `absint()` first
	// defends against a code-editor override (the attribute can be set
	// to any JSON value there); the `min`/`max` then keep the value
	// within the range where a `&.columns-#{$i}` rule actually exists.
	$column_count = min( 6, max( 2, absint( $attributes['columnCount'] ?? 5 ) ) );

	wp_enqueue_script_module( $this->get_full_block_name() );

	$consent = 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WooCommerce';
	BlocksSharedState::load_store_config( $consent );
	BlocksSharedState::load_placeholder_image( $consent );
	// `Add to cart` calls into the shared cart store, which expects
	// `state.cart.items` and friends. Without this load the cart store
	// would have no hydrated cart and the action would throw on the
	// first click.
	BlocksSharedState::load_cart_state( $consent );

	$items = $this->prefetch_items();

	// Seed the shared shopper-lists store with the rest URL, the
	// pre-fetched items, and a starter nonce. The starter nonce is
	// what the cart store also seeds via `state.nonce` — the JS layer
	// keeps it fresh by reading the `Nonce` response header on every
	// subsequent request, so this is just the bootstrap value (and
	// avoids deadlocking mutations that await `isNonceReady` before
	// any GET has fired).
	wp_interactivity_state(
		'woocommerce/shopper-lists',
		array(
			'restUrl' => get_rest_url(),
			'nonce'   => wp_create_nonce( 'wc_store_api' ),
			'lists'   => array(
				self::LIST_SLUG => array(
					'items'     => $items,
					'isLoading' => false,
				),
			),
		)
	);

	// Only the remove-button aria-label template needs JS-side
	// interpolation; visible strings (empty state, action label) are
	// rendered server-side and toggled with directives.
	wp_interactivity_config(
		'woocommerce/wishlist',
		array(
			'removeLabelTemplate' => $this->get_remove_label_template(),
		)
	);

	// No `hasShownItems` flag: unlike Saved for Later (which auto-
	// renders on every cart visit and must avoid flashing an empty
	// message before a runtime save lands), Wishlist is reached
	// deliberately — by the My Account endpoint or because a merchant
	// placed it. Showing the empty message immediately is the right
	// signal: the visitor came to look at their wishlist, and it's
	// empty. `data-wp-context---notices` seeds the store-notices
	// namespace alongside the block's own context on the same wrapper.
	$wrapper_attributes = array(
		'class'                     => 'wc-block-wishlist',
		'data-wp-interactive'       => 'woocommerce/wishlist',
		'data-wp-context'           => (string) wp_json_encode(
			array(
				// `stdClass` so it serialises as `{}`, not `[]` —
				// iAPI's reactive proxy only fires updates on object
				// writes, not array expandos.
				'pendingKeys' => new \stdClass(),
			)
		),
		'data-wp-context---notices' => 'woocommerce/store-notices::' . (string) wp_json_encode( array( 'notices' => array() ) ),
	);

	$list_class  = sprintf( 'wc-block-wishlist__list columns-%d', $column_count );
	$ul_inner    = $this->render_template_markup() . $this->render_items_markup( $items ) . $this->render_empty_markup( $items );
	$before_list = $this->render_header_markup( $content ) . ShopperListRenderer::render_interactivity_notices_region( 'wc-block-wishlist__notices' );

	return ShopperListRenderer::render_grid_wrapper( $wrapper_attributes, $list_class, $ul_inner, $before_list );
}