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
Returns
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 10.5.0
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;
}
$providers_payload = [];
if ( class_exists( AddressProviderController::class ) && 'no' !== get_option( 'woocommerce_address_autocomplete_enabled', 'no' ) ) {
$controller = wc_get_container()->get( AddressProviderController::class );
$providers = $controller->get_providers();
$providers_payload = array_map(
static function ( $provider ) {
return array(
'id' => (string) $provider->id,
'name' => sanitize_text_field( (string) $provider->name ),
'branding_html' => wp_kses_post( (string) $provider->branding_html ),
);
},
(array) $providers
);
}
$this->asset_data_registry->add( 'addressAutocompleteProviders', $providers_payload );
$this->asset_data_registry->add( 'countryData', $country_data );
$this->asset_data_registry->add( 'defaultAddressFormat', $address_formats['default'] );
$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', wp_is_block_theme() );
$this->asset_data_registry->add( 'isCheckoutBlock', true );
$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( 'localPickupCost', $pickup_location_settings['cost'] );
$this->asset_data_registry->add( 'collectableMethodIds', $local_pickup_method_ids );
$this->asset_data_registry->add( 'shippingMethodsExist', CartCheckoutUtils::shipping_methods_exist() );
$is_block_editor = $this->is_block_editor();
if ( $is_block_editor ) {
$this->asset_data_registry->add(
'localPickupLocations',
array_filter(
array_map(
function ( $location ) {
if ( ! wc_string_to_bool( $location['enabled'] ) ) {
return null;
}
$location['formatted_address'] = wc()->countries->get_formatted_address( $location['address'], ', ' );
return $location;
},
LocalPickupUtils::get_local_pickup_method_locations()
)
),
);
}
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 = PaymentUtils::get_enabled_payment_gateways();
$formatted_payment_methods = array_reduce(
$payment_methods,
function ( $acc, $method ) {
$acc[] = [
'id' => $method->id,
'title' => $method->get_method_title() !== '' ? $method->get_method_title() : $method->get_title(),
'description' => $method->get_method_description() !== '' ? $method->get_method_description() : $method->get_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' );
}