Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
VariationSelectorAttribute::render_attribute_row
Get attribute row HTML.
Method of the class: VariationSelectorAttribute{}
No Hooks.
Returns
String. Row HTML
Usage
// private - for code of main (parent) class only $result = $this->render_attribute_row( $attribute_name, $product_attribute_terms, $block, $attributes, $available_values_by_attribute ): string;
- $attribute_name(string) (required)
- Product Attribute Name.
- $product_attribute_terms(array) (required)
- Product Attribute Terms.
- $block(WP_Block) (required)
- The Block.
- $attributes(array) (required)
- Template block attributes (displayStyle, autoselect, etc.).
- $available_values_by_attribute(array) (required)
- Variation values keyed by attribute slug.
VariationSelectorAttribute::render_attribute_row() VariationSelectorAttribute::render attribute row code WC 10.9.1
private function render_attribute_row( string $attribute_name, array $product_attribute_terms, WP_Block $block, array $attributes, array $available_values_by_attribute ): string {
$inner_blocks = $block->parsed_block['innerBlocks'] ?? array();
if ( empty( $inner_blocks ) ) {
return '';
}
$attribute_slug = wc_variation_attribute_name( $attribute_name );
$attribute_terms = $this->get_filtered_attribute_terms(
$attribute_name,
$product_attribute_terms,
$available_values_by_attribute[ $attribute_slug ] ?? array()
);
if ( empty( $attribute_terms ) ) {
return '';
}
$default_selected = $this->get_default_selected_attribute( $attribute_slug, $attribute_terms );
$variation_items = $this->build_variation_selectable_items( $attribute_name, $attribute_slug, $attribute_terms, $default_selected );
$attribute_label = wc_attribute_label( $attribute_name );
$attribute_id = 'wc_product_attribute_' . uniqid();
$context = array(
'woocommerce/attributeId' => $attribute_id,
'woocommerce/attributeName' => $attribute_name,
'woocommerce/attributeTerms' => $attribute_terms,
'woocommerce/selectableItems' => array(
'items' => $variation_items,
'selectionMode' => 'single',
'storeNamespace' => 'woocommerce/add-to-cart-with-options',
'groupLabel' => $attribute_label,
),
);
$inner_html = '';
foreach ( $inner_blocks as $inner_block ) {
$inner_block = $this->replace_legacy_attribute_options_block( $inner_block, $attributes );
$inner_html .= ( new WP_Block( $inner_block, $context ) )->render();
}
$interactive_context = array(
'name' => $attribute_label,
'variationAttributeOptions' => $variation_items,
'selectedValue' => $default_selected,
'autoselect' => $attributes['autoselect'] ?? false,
'disabledAttributesAction' => $attributes['disabledAttributesAction'] ?? 'disable',
);
$interactive_attributes = array(
'data-wp-interactive' => 'woocommerce/add-to-cart-with-options',
'data-wp-init' => 'callbacks.setDefaultSelectedAttribute',
);
// Hidden input for legacy form POST submissions (page refresh). Chips and
// dropdown UI elements do not include name="attribute_*" fields.
$hidden_attribute_input = sprintf(
'<input type="hidden" name="%1$s" value="%2$s" data-wp-bind--value="context.selectedValue" />',
esc_attr( $attribute_slug ),
esc_attr( $default_selected ?? '' )
);
return sprintf(
'<div %s %s>%s%s</div>',
get_block_wrapper_attributes( $interactive_attributes ),
wp_interactivity_data_wp_context( $interactive_context ),
$inner_html,
$hidden_attribute_input
);
}