Automattic\WooCommerce\Blocks\Utils

BlocksSharedState::load_cart_statepublic staticWC 1.0

Load cart state into interactivity state.

Method of the class: BlocksSharedState{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = BlocksSharedState::load_cart_state( $consent_statement ): void;
$consent_statement(string) (required)
The consent statement string.

BlocksSharedState::load_cart_state() code WC 10.8.1

public static function load_cart_state( string $consent_statement ): void {
	self::check_consent( $consent_statement );

	if ( null === self::$blocks_shared_cart_state ) {
		$cart_exists       = isset( WC()->cart );
		$cart_has_contents = $cart_exists && ! WC()->cart->is_empty();
		if ( $cart_exists ) {
			$cart_response                  = Package::container()->get( Hydration::class )->get_rest_api_response_data( '/wc/store/v1/cart' );
			self::$blocks_shared_cart_state = $cart_response['body'] ?? array();
		} else {
			self::$blocks_shared_cart_state = array();
		}

		if ( $cart_has_contents ) {
			self::prevent_cache();
		}

		wp_interactivity_config(
			self::$settings_namespace,
			array( 'nonOptimisticProperties' => self::get_non_optimistic_properties() )
		);

		wp_interactivity_state(
			self::$settings_namespace,
			array(
				'cart'     => self::$blocks_shared_cart_state,
				'noticeId' => '',
				'restUrl'  => get_rest_url(),
			)
		);
	}
}