wp_interactivity_data_wp_context(
array(
'productId' => $product_id,
'variationId' => null,
),
'woocommerce/products'
);
$li_directives = '
data-wp-interactive="woocommerce/product-collection"
' . $product_context_directive . '
data-wp-key="product-item-' . $product_id . '"
';
// Wrap the render inner blocks in a `li` element with the appropriate post classes.
$post_classes = implode( ' ', get_post_class( 'wc-block-product' ) );
$content .= strtr(
'
{content}
',
array(
'{classes}' => esc_attr( $post_classes ),
'{li_directives}' => $li_directives,
'{content}' => $block_content,
)
);
}
/*
* Use this function to restore the context of the template tags
* from a secondary query loop back to the main query loop.
* Since we use two custom loops, it's safest to always restore.
*/
wp_reset_postdata();
return sprintf(
'',
$wrapper_attributes,
$content
);
}
/**
* Determines whether a block list contains a block that uses the featured image.
*
* @param WP_Block_List $inner_blocks Inner block instance.
*
* @return bool Whether the block list contains a block that uses the featured image.
*/
protected function block_core_post_template_uses_featured_image( $inner_blocks ) {
foreach ( $inner_blocks as $block ) {
if ( 'core/post-featured-image' === $block->name ) {
return true;
}
if (
'core/cover' === $block->name &&
! empty( $block->attributes['useFeaturedImage'] )
) {
return true;
}
if ( $block->inner_blocks && block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
return true;
}
}
return false;
}
/**
* Product Template renders inner blocks manually so we need to skip default
* rendering routine for its inner blocks
*
* @param array $settings Array of determined settings for registering a block type.
* @param array $metadata Metadata provided for registering a block type.
* @return array
*/
public function add_block_type_metadata_settings( $settings, $metadata ) {
if ( ! empty( $metadata['name'] ) && 'woocommerce/product-template' === $metadata['name'] ) {
$settings['skip_inner_blocks'] = true;
}
return $settings;
}
}