Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
VariationSelectorAttribute::get_product_row
Get product row HTML.
Method of the class: VariationSelectorAttribute{}
No Hooks.
Returns
String. Row HTML
Usage
// private - for code of main (parent) class only $result = $this->get_product_row( $attribute_name, $product_attribute_terms, $block ): string;
- $attribute_name(string) (required)
- Product Attribute Name.
- $product_attribute_terms(array) (required)
- Product Attribute Terms.
- $block(WP_Block) (required)
- The Block.
VariationSelectorAttribute::get_product_row() VariationSelectorAttribute::get product row code WC 10.7.0
private function get_product_row( $attribute_name, $product_attribute_terms, $block ): string {
global $product;
$attribute_terms = $this->get_terms( $attribute_name, $product_attribute_terms );
$product_variations = $product->get_available_variations( 'objects' );
// Filter out terms which are not available in any product variation.
$attribute_terms = array_filter(
$attribute_terms,
function ( $term ) use ( $product_variations, $attribute_name ) {
foreach ( $product_variations as $variation ) {
$attributes = $variation->get_variation_attributes();
if (
$term['value'] === $attributes[ wc_variation_attribute_name( $attribute_name ) ] ||
'' === $attributes[ wc_variation_attribute_name( $attribute_name ) ]
) {
return true;
}
}
}
);
if ( empty( $attribute_terms ) ) {
return '';
}
$block_content = AddToCartWithOptionsUtils::render_block_with_context(
$block,
array(
'woocommerce/attributeId' => 'wc_product_attribute_' . uniqid(),
'woocommerce/attributeName' => $attribute_name,
'woocommerce/attributeTerms' => $attribute_terms,
),
);
// Render the inner blocks of the Variation Selector Item Template block with `dynamic` set to `false`
// to prevent calling `render_callback` and ensure that no wrapper markup is included.
return $block_content;
}