WooCommerce Code Reference

VariationSelectorAttribute extends AbstractBlock
in package
Uses EnableBlockJsonAssetsTrait

Block type for Variation Selector in the Add to Cart + Options block.

Table of Contents

$asset_api  : Api
Instance of the asset API.
$asset_data_registry  : AssetDataRegistry
Instance of the asset data registry.
$block_name  : string
Block name.
$enqueued_assets  : bool
Tracks if assets have been enqueued.
$integration_registry  : IntegrationRegistry
Instance of the integration registry.
$namespace  : string
Block namespace.
__construct()  : mixed
Constructor.
enqueue_editor_assets()  : mixed
Enqueue assets used for rendering the block in editor context.
render_callback()  : string
The default render_callback for all blocks. This will ensure assets are enqueued just in time, then render the block (if applicable).
enqueue_data()  : void
Extra data passed through from server to client for block.
enqueue_scripts()  : mixed
Register/enqueue scripts used for this block on the frontend, during render.
get_block_type()  : string
Get the block type.
get_block_type_attributes()  : array<string|int, mixed>
Get block attributes.
get_block_type_editor_script()  : array<string|int, mixed>|string
Get the editor script data for this block type.
get_block_type_editor_style()  : string|null
Get the editor style handle for this block type.
get_block_type_render_callback()  : callable|null
Get the render callback for this block type.
get_block_type_script()  : array<string|int, mixed>|string|null
Get the frontend script handle for this block type.
get_block_type_style()  : array<string|int, string>|null
Get the frontend style handle for this block type.
get_block_type_supports()  : string
Get the supports array for this block type.
get_block_type_uses_context()  : array<string|int, mixed>
Get block usesContext.
get_chunks_paths()  : array<string|int, string>
Generate an array of chunks paths for loading translation.
get_full_block_name()  : string
Get the full block name, including namespace.
get_routes_from_namespace()  : array<string|int, mixed>
Get routes from a REST API namespace.
initialize()  : mixed
Initialize this block type.
is_block_editor()  : mixed
Are we currently on the admin block editor screen?
parse_render_callback_attributes()  : array<string|int, mixed>
Parses block attributes from the render_callback.
register_block_type()  : array<string|int, string>
Registers the block type with WordPress.
register_block_type_assets()  : mixed
Register script and style assets for the block type before it is registered.
register_chunk_translations()  : mixed
Injects Chunk Translations into the page so translations work for lazy loaded components.
render()  : string
Render the block.
build_variation_selectable_items()  : array<string|int, mixed>
Build selectable items for the inner block protocol and client context.
get_available_variation_values_by_attribute_slug()  : array<string|int, mixed>
Build a lookup of attribute values used by available variations.
get_default_selected_attribute()  : string|null
Get the default selected attribute.
get_filtered_attribute_terms()  : array<string|int, mixed>
Build filtered attribute term options for the variation selector.
map_term_to_option()  : array<string|int, mixed>
Map a taxonomy term or custom attribute option to the variation row option shape.
render_attribute_row()  : string
Get attribute row HTML.
replace_legacy_attribute_options_block()  : array<string|int, mixed>
Replace legacy Attribute Options block and apply its settings to the parent attributes.

Properties

Methods

__construct()

Constructor.

public __construct(Api $asset_api, AssetDataRegistry $asset_data_registry, IntegrationRegistry $integration_registry[, string $block_name = '' ]) : mixed
Parameters
$asset_api : Api

Instance of the asset API.

$asset_data_registry : AssetDataRegistry

Instance of the asset data registry.

$integration_registry : IntegrationRegistry

Instance of the integration registry.

$block_name : string = ''

Optionally set block name during construct.

Return values
mixed

enqueue_editor_assets()

Enqueue assets used for rendering the block in editor context.

public enqueue_editor_assets() : mixed

This is needed if a block is not yet within the post content--render and enqueue_assets may not have ran.

Return values
mixed

render_callback()

The default render_callback for all blocks. This will ensure assets are enqueued just in time, then render the block (if applicable).

public render_callback([array<string|int, mixed>|WP_Block $attributes = [] ][, string $content = '' ][, WP_Block|null $block = null ]) : string
Parameters
$attributes : array<string|int, mixed>|WP_Block = []

Block attributes, or an instance of a WP_Block. Defaults to an empty array.

$content : string = ''

Block content. Default empty string.

$block : WP_Block|null = null

Block instance.

Return values
stringRendered block type output.

enqueue_data()

Extra data passed through from server to client for block.

protected enqueue_data([array<string|int, mixed> $attributes = array() ]) : void
Parameters
$attributes : array<string|int, mixed> = 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.

Return values
void

enqueue_scripts()

Register/enqueue scripts used for this block on the frontend, during render.

protected enqueue_scripts([array<string|int, mixed> $attributes = [] ]) : mixed
Parameters
$attributes : array<string|int, mixed> = []

Any attributes that currently are available from the block.

Return values
mixed

get_block_type()

Get the block type.

protected get_block_type() : string
Tags
deprecated
10.9.0

Use get_full_block_name() instead.

Return values
string

get_block_type_attributes()

Get block attributes.

protected get_block_type_attributes() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_block_type_editor_script()

Get the editor script data for this block type.

protected get_block_type_editor_script([string $key = null ]) : array<string|int, mixed>|string
Parameters
$key : string = null

Data to get, or default to everything.

Tags
see

$this->register_block_type()

Return values
array<string|int, mixed>|string

get_block_type_editor_style()

Get the editor style handle for this block type.

protected get_block_type_editor_style() : string|null
Tags
see

$this->register_block_type()

Return values
string|null

get_block_type_render_callback()

Get the render callback for this block type.

protected get_block_type_render_callback() : callable|null

Dynamic blocks should return a callback, for example, return [ $this, 'render' ];

Tags
see

$this->register_block_type()

Return values
callable|null

get_block_type_script()

Get the frontend script handle for this block type.

protected get_block_type_script([string $key = null ]) : array<string|int, mixed>|string|null
Parameters
$key : string = null

Data to get, or default to everything.

Tags
see

$this->register_block_type()

Return values
array<string|int, mixed>|string|null

get_block_type_style()

Get the frontend style handle for this block type.

protected get_block_type_style() : array<string|int, string>|null
Return values
array<string|int, string>|null

get_block_type_supports()

Get the supports array for this block type.

protected get_block_type_supports() : string
Tags
see

$this->register_block_type()

Return values
string

get_block_type_uses_context()

Get block usesContext.

protected get_block_type_uses_context() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_chunks_paths()

Generate an array of chunks paths for loading translation.

protected get_chunks_paths(string $chunks_folder) : array<string|int, string>
Parameters
$chunks_folder : string

The folder to iterate over.

Return values
array<string|int, string>$chunks list of chunks to load.

get_routes_from_namespace()

Get routes from a REST API namespace.

protected get_routes_from_namespace(string $namespace) : array<string|int, mixed>
Parameters
$namespace : string

Namespace to retrieve.

Return values
array<string|int, mixed>

initialize()

Initialize this block type.

protected initialize() : mixed
  • Hook into WP lifecycle.
  • Register the block with WordPress.
Return values
mixed

parse_render_callback_attributes()

Parses block attributes from the render_callback.

protected parse_render_callback_attributes(array<string|int, mixed>|WP_Block $attributes) : array<string|int, mixed>
Parameters
$attributes : array<string|int, mixed>|WP_Block

Block attributes, or an instance of a WP_Block. Defaults to an empty array.

Return values
array<string|int, mixed>

register_block_type()

Registers the block type with WordPress.

protected register_block_type() : array<string|int, string>
Return values
array<string|int, string>Chunks paths.

register_block_type_assets()

Register script and style assets for the block type before it is registered.

protected register_block_type_assets() : mixed

This registers the scripts; it does not enqueue them.

Return values
mixed

register_chunk_translations()

Injects Chunk Translations into the page so translations work for lazy loaded components.

protected register_chunk_translations(array<string|int, string> $chunks) : mixed

The chunk names are defined when creating lazy loaded components using webpackChunkName.

Parameters
$chunks : array<string|int, string>

Array of chunk names.

Return values
mixed

render()

Render the block.

protected render(array<string|int, mixed> $attributes, string $content, WP_Block $block) : string
Parameters
$attributes : array<string|int, mixed>

Block attributes.

$content : string

Block content.

$block : WP_Block

Block instance.

Return values
stringRendered block output.

build_variation_selectable_items()

Build selectable items for the inner block protocol and client context.

private build_variation_selectable_items(string $attribute_name, string $attribute_slug, array<string|int, mixed> $attribute_terms, string|null $default_selected) : array<string|int, mixed>
Parameters
$attribute_name : string

Product attribute name.

$attribute_slug : string

Attribute slug.

$attribute_terms : array<string|int, mixed>

Terms from context.

$default_selected : string|null

Default selected attribute value.

Return values
array<string|int, mixed>

get_available_variation_values_by_attribute_slug()

Build a lookup of attribute values used by available variations.

private get_available_variation_values_by_attribute_slug() : array<string|int, mixed>
Return values
array<string|int, mixed>Map of attribute slug to set of values (keys are values).

get_default_selected_attribute()

Get the default selected attribute.

private get_default_selected_attribute(string $attribute_slug, array<string|int, mixed> $attribute_terms) : string|null
Parameters
$attribute_slug : string

The attribute's slug.

$attribute_terms : array<string|int, mixed>

The attribute's terms.

Return values
string|nullThe default selected attribute.

get_filtered_attribute_terms()

Build filtered attribute term options for the variation selector.

private get_filtered_attribute_terms(string $attribute_name, array<string|int, mixed> $product_attribute_terms, array<string|int, mixed> $available_values) : array<string|int, mixed>
Parameters
$attribute_name : string

Product attribute name.

$product_attribute_terms : array<string|int, mixed>

Custom attribute terms when not a taxonomy.

$available_values : array<string|int, mixed>

Available variation values for this attribute (value => true).

Return values
array<string|int, mixed>Attribute terms, or empty string when none match.

map_term_to_option()

Map a taxonomy term or custom attribute option to the variation row option shape.

private map_term_to_option(WP_Term|string $term, string $attribute_name, WC_Product $product, string $selected_attribute) : array<string|int, mixed>
Parameters
$term : WP_Term|string

Term object for taxonomies, option string for custom attributes.

$attribute_name : string

Name of the attribute.

$product : WC_Product

Product object.

$selected_attribute : string

Default selected attribute value.

Return values
array<string|int, mixed>

render_attribute_row()

Get attribute row HTML.

private render_attribute_row(string $attribute_name, array<string|int, mixed> $product_attribute_terms, WP_Block $block, array<string|int, mixed> $attributes, array<string|int, mixed> $available_values_by_attribute) : string
Parameters
$attribute_name : string

Product Attribute Name.

$product_attribute_terms : array<string|int, mixed>

Product Attribute Terms.

$block : WP_Block

The Block.

$attributes : array<string|int, mixed>

Template block attributes (displayStyle, autoselect, etc.).

$available_values_by_attribute : array<string|int, mixed>

Variation values keyed by attribute slug.

Return values
stringRow HTML

replace_legacy_attribute_options_block()

Replace legacy Attribute Options block and apply its settings to the parent attributes.

private replace_legacy_attribute_options_block(array<string|int, mixed> $inner_block, array<string|int, mixed> &$attributes) : array<string|int, mixed>
Parameters
$inner_block : array<string|int, mixed>

The inner block to replace.

$attributes : array<string|int, mixed>

Parent block attributes, updated when attributes in the legacy Attribute Options block are found.

Return values
array<string|int, mixed>The replaced inner block.