Automattic\WooCommerce\Blocks\BlockTypes
Checkout::enqueue_data() │ protected │ WC 1.0
Extra data passed through from server to client for block.
Method of the class: Checkout{}
Hooks from the method
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() Checkout::enqueue data code WC 9.4.2
protected function enqueue_data( array $attributes = [] ) { parent::enqueue_data( $attributes ); $country_data = CartCheckoutUtils::get_country_data(); $address_formats = WC()->countries->get_address_formats(); // Move the address format into the 'countryData' setting. // We need to skip 'default' because that's not a valid country. foreach ( $address_formats as $country_code => $format ) { if ( 'default' === $country_code ) { continue; } $country_data[ $country_code ]['format'] = $format; } $this->asset_data_registry->add( 'countryData', $country_data ); $this->asset_data_registry->add( 'defaultAddressFormat', $address_formats['default'] ); $this->asset_data_registry->add( 'baseLocation', wc_get_base_location() ); $this->asset_data_registry->add( 'checkoutAllowsGuest', false === filter_var( wc()->checkout()->is_registration_required(), FILTER_VALIDATE_BOOLEAN ) ); $this->asset_data_registry->add( 'checkoutAllowsSignup', filter_var( wc()->checkout()->is_registration_enabled(), FILTER_VALIDATE_BOOLEAN ) ); $this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ) ); $this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ) ); $this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ) ); $this->asset_data_registry->add( 'forcedBillingAddress', 'billing_only' === get_option( 'woocommerce_ship_to_destination' ) ); $this->asset_data_registry->add( 'generatePassword', filter_var( get_option( 'woocommerce_registration_generate_password' ), FILTER_VALIDATE_BOOLEAN ) ); $this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled() ); $this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled() ); $this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled() ); $this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ) ); $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() ); $pickup_location_settings = LocalPickupUtils::get_local_pickup_settings(); $local_pickup_method_ids = LocalPickupUtils::get_local_pickup_method_ids(); $this->asset_data_registry->add( 'localPickupEnabled', $pickup_location_settings['enabled'] ); $this->asset_data_registry->add( 'localPickupText', $pickup_location_settings['title'] ); $this->asset_data_registry->add( 'collectableMethodIds', $local_pickup_method_ids ); $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 ) use ( $local_pickup_method_ids ) { if ( in_array( $method->id, $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' ); }