Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

HandlerRegistry::get_cart_product_idsprivateWC 1.0

Get cart product IDs from various sources. Handles loading cart products from location context or request params.

Method of the class: HandlerRegistry{}

No Hooks.

Returns

Array. The product IDs from the cart. Returns recent products for preview in editor context only.

Usage

// private - for code of main (parent) class only
$result = $this->get_cart_product_ids( $request );
$request(WP_REST_Request|null)
Optional REST request for editor context.
Default: null

HandlerRegistry::get_cart_product_ids() code WC 10.9.1

private function get_cart_product_ids( $request = null ) {
	if ( $request ) {
		// In editor context (REST request), show sample products for preview. Only emails to the customer show live data.
		$recent_product_ids = wc_get_products(
			array(
				'status'  => 'publish',
				'orderby' => 'date',
				'order'   => 'DESC',
				'limit'   => 3,
				'return'  => 'ids',
			)
		);
		return ! empty( $recent_product_ids ) ? $recent_product_ids : array();
	}

	// In frontend/email context, return empty array when no cart is found.
	return array();
}