Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks
Product_Button::render_content
Render the product button block content for email.
Method of the class: Product_Button{}
No Hooks.
Returns
String.
Usage
// protected - for code of main (parent) or child class $result = $this->render_content( $block_content, $parsed_block, $rendering_context ): string;
- $block_content(string) (required)
- Block content.
- $parsed_block(array) (required)
- Parsed block.
- $rendering_context(Rendering_Context) (required)
- Rendering context.
Product_Button::render_content() Product Button::render content code WC 10.5.0
protected function render_content( string $block_content, array $parsed_block, Rendering_Context $rendering_context ): string {
$product = $this->get_product_from_context( $parsed_block );
if ( ! $product ) {
return '';
}
// Check if this is a cart-contents collection to customize button text and link.
$collection = $parsed_block['context']['collection'] ?? '';
$is_cart_contents = 'woocommerce/product-collection/cart-contents' === $collection;
if ( $is_cart_contents ) {
// For cart contents, link to cart page instead of product page.
$button_text = __( 'Finish checkout', 'woocommerce' );
$button_url = wc_get_cart_url();
} else {
$button_text = $product->add_to_cart_text() ? $product->add_to_cart_text() : __( 'Add to cart', 'woocommerce' );
if ( $product->is_type( 'external' ) && $product instanceof \WC_Product_External ) {
$external_url = $product->get_product_url();
$button_url = $external_url ? $external_url : $product->get_permalink();
} else {
$button_url = $product->get_permalink();
}
}
$block_attributes = array_replace_recursive(
array(
'textColor' => '#ffffff',
'backgroundColor' => '#000000',
'textAlign' => 'left',
'width' => '',
'style' => array(
'typography' => array(
'fontSize' => '16px',
'fontWeight' => 'bold',
),
'border' => array(
'radius' => '0',
),
'spacing' => array(
'padding' => '12px 24px',
),
),
),
$parsed_block['attrs'] ?? array()
);
$wrapper_styles = $this->get_wrapper_styles( $block_attributes, $rendering_context );
$button_styles = $this->get_button_styles( $block_attributes, $rendering_context );
$table_attrs = array(
'style' => 'width:' . ( $block_attributes['width'] ? '100%' : 'auto' ) . ';',
'align' => $block_attributes['textAlign'],
);
$cell_attrs = array(
'class' => $wrapper_styles['classnames'],
'style' => $wrapper_styles['css'],
'align' => $block_attributes['textAlign'],
'valign' => 'middle',
'role' => 'presentation',
);
$button_content = sprintf(
'<a class="product-button-link %1$s" style="%2$s" href="%3$s" target="_blank">%4$s</a>',
esc_attr( $button_styles['classnames'] ),
esc_attr( $button_styles['css'] ),
esc_url( $button_url ),
esc_html( $button_text )
);
$button_html = Table_Wrapper_Helper::render_table_wrapper( $button_content, $table_attrs, $cell_attrs );
return Table_Wrapper_Helper::render_table_wrapper( $button_html, array( 'style' => 'width: 100%' ) );
}