Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks
Product_Collection::render_product_content
Render default product content when no inner blocks are present.
Method of the class: Product_Collection{}
No Hooks.
Returns
String.
Usage
// private - for code of main (parent) class only $result = $this->render_product_content( ?\WC_Product $product, $template_block, $collection_type, ?int $cell_width ): string;
- ?\WC_Product $product(required)
- .
- $template_block(array) (required)
- Inner block data.
- $collection_type(string) (required)
- Collection type identifier.
- ?int $cell_width
- .
Default:null
Product_Collection::render_product_content() Product Collection::render product content code WC 10.8.1
private function render_product_content( ?\WC_Product $product, array $template_block, string $collection_type, ?int $cell_width = null ): string {
$content = '';
if ( ! $product ) {
return $content;
}
$inner_index = 0;
foreach ( $template_block['innerBlocks'] as $inner_block ) {
// Override the preprocessor-applied blockGap margin-top for inner blocks.
// The editor does not vary spacing between inner product elements
// (image, title, price) when blockGap changes, so we use a fixed value
// to keep editor and preview consistent.
$inner_block['email_attrs'] = $inner_block['email_attrs'] ?? array();
if ( 0 === $inner_index ) {
unset( $inner_block['email_attrs']['margin-top'] );
} else {
$inner_block['email_attrs']['margin-top'] = self::INNER_BLOCK_SPACING;
}
// Set cell width context for multi-column layouts.
if ( null !== $cell_width ) {
$inner_block['email_attrs']['width'] = $cell_width . 'px';
}
++$inner_index;
switch ( $inner_block['blockName'] ) {
case 'woocommerce/product-price':
case 'woocommerce/product-button':
case 'woocommerce/product-sale-badge':
case 'woocommerce/product-image':
$inner_block['context'] = $inner_block['context'] ?? array();
$inner_block['context']['postId'] = $product->get_id();
$inner_block['context']['collection'] = $collection_type;
$content .= render_block( $inner_block );
break;
case 'core/post-title':
global $post;
$original_post = $post;
$original_global_product = $GLOBALS['product'] ?? null;
$product_post = get_post( $product->get_id() );
$post = $product_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$GLOBALS['product'] = $product;
$inner_block['context'] = $inner_block['context'] ?? array();
$inner_block['context']['postId'] = $product->get_id();
$content .= render_block( $inner_block );
$post = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$GLOBALS['product'] = $original_global_product;
break;
default:
break;
}
}
return $content;
}