Automattic\WooCommerce\Blocks\BlockTypes

Checkout::enqueue_data()protectedWC 1.0

Extra data passed through from server to client for block.

Method of the class: Checkout{}

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->enqueue_data( $attributes );
$attributes(array)
Any attributes that currently are available from the block. Note, this will be empty in the editor context when the block is not in the post content on editor load.
Default: []

Checkout::enqueue_data() code WC 8.7.0

protected function enqueue_data( array $attributes = [] ) {
	parent::enqueue_data( $attributes );

	$this->asset_data_registry->add( 'countryData', CartCheckoutUtils::get_country_data(), true );
	$this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true );
	$this->asset_data_registry->add(
		'checkoutAllowsGuest',
		false === filter_var(
			wc()->checkout()->is_registration_required(),
			FILTER_VALIDATE_BOOLEAN
		),
		true
	);
	$this->asset_data_registry->add(
		'checkoutAllowsSignup',
		filter_var(
			wc()->checkout()->is_registration_enabled(),
			FILTER_VALIDATE_BOOLEAN
		),
		true
	);
	$this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true );
	$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true );
	$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true );
	$this->asset_data_registry->add( 'forcedBillingAddress', 'billing_only' === get_option( 'woocommerce_ship_to_destination' ), true );
	$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true );
	$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true );
	$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true );
	$this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true );
	$this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 );
	$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );

	$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
	$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true );

	$is_block_editor = $this->is_block_editor();

	// Hydrate the following data depending on admin or frontend context.
	if ( $is_block_editor && ! $this->asset_data_registry->exists( 'shippingMethodsExist' ) ) {
		$methods_exist = wc_get_shipping_method_count( false, true ) > 0;
		$this->asset_data_registry->add( 'shippingMethodsExist', $methods_exist );
	}

	if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalShippingMethods' ) ) {
		$shipping_methods           = WC()->shipping()->get_shipping_methods();
		$formatted_shipping_methods = array_reduce(
			$shipping_methods,
			function( $acc, $method ) {
				if ( in_array( $method->id, LocalPickupUtils::get_local_pickup_method_ids(), true ) ) {
					return $acc;
				}
				if ( $method->supports( 'settings' ) ) {
					$acc[] = [
						'id'          => $method->id,
						'title'       => $method->method_title,
						'description' => $method->method_description,
					];
				}
				return $acc;
			},
			[]
		);
		$this->asset_data_registry->add( 'globalShippingMethods', $formatted_shipping_methods );
	}

	if ( $is_block_editor && ! $this->asset_data_registry->exists( 'activeShippingZones' ) && class_exists( '\WC_Shipping_Zones' ) ) {
		$this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones() );
	}

	if ( $is_block_editor && ! $this->asset_data_registry->exists( 'globalPaymentMethods' ) ) {
		// These are used to show options in the sidebar. We want to get the full list of enabled payment methods,
		// not just the ones that are available for the current cart (which may not exist yet).
		$payment_methods           = $this->get_enabled_payment_gateways();
		$formatted_payment_methods = array_reduce(
			$payment_methods,
			function( $acc, $method ) {
				$acc[] = [
					'id'          => $method->id,
					'title'       => $method->method_title,
					'description' => $method->method_description,
				];
				return $acc;
			},
			[]
		);
		$this->asset_data_registry->add( 'globalPaymentMethods', $formatted_payment_methods );
	}

	if ( $is_block_editor && ! $this->asset_data_registry->exists( 'incompatibleExtensions' ) ) {
		if ( ! class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) || ! function_exists( 'get_plugins' ) ) {
			return;
		}

		$declared_extensions     = \Automattic\WooCommerce\Utilities\FeaturesUtil::get_compatible_plugins_for_feature( 'cart_checkout_blocks' );
		$all_plugins             = \get_plugins(); // Note that `get_compatible_plugins_for_feature` calls `get_plugins` internally, so this is already in cache.
		$incompatible_extensions = array_reduce(
			$declared_extensions['incompatible'],
			function( $acc, $item ) use ( $all_plugins ) {
				$plugin      = $all_plugins[ $item ] ?? null;
				$plugin_id   = $plugin['TextDomain'] ?? dirname( $item, 2 );
				$plugin_name = $plugin['Name'] ?? $plugin_id;
				$acc[]       = [
					'id'    => $plugin_id,
					'title' => $plugin_name,
				];
				return $acc;
			},
			[]
		);
		$this->asset_data_registry->add( 'incompatibleExtensions', $incompatible_extensions );
	}

	if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
		$this->asset_data_registry->hydrate_api_request( '/wc/store/v1/cart' );
		$this->asset_data_registry->hydrate_data_from_api_request( 'checkoutData', '/wc/store/v1/checkout' );
		$this->hydrate_customer_payment_methods();
	}

	/**
	 * Fires after checkout block data is registered.
	 *
	 * @since 2.6.0
	 */
	do_action( 'woocommerce_blocks_checkout_enqueue_data' );
}