Automattic\WooCommerce\Blocks\BlockTypes
ProductSaleBadge::render
Include and render the block.
Method of the class: ProductSaleBadge{}
Hooks from the method
Returns
String. Rendered block type output.
Usage
// protected - for code of main (parent) or child class $result = $this->render( $attributes, $content, $block );
- $attributes(array) (required)
- Block attributes.
Default:empty array - $content(string) (required)
- Block content.
Default:empty string - $block(WP_Block) (required)
- Block instance.
ProductSaleBadge::render() ProductSaleBadge::render code WC 10.5.0
protected function render( $attributes, $content, $block ) {
$post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : '';
$product = wc_get_product( $post_id );
if ( ! $product ) {
return null;
}
$is_on_sale = $product->is_on_sale();
if ( ! $is_on_sale ) {
return null;
}
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'extra_classes' ) );
$classname = StyleAttributesUtils::get_classes_by_attributes( $attributes, array( 'extra_classes' ) );
$align = isset( $attributes['align'] ) ? $attributes['align'] : '';
/**
* Filters the product sale badge text.
*
* @hook woocommerce_sale_badge_text
* @since 10.0.0
*
* @param string $sale_text The sale badge text.
* @param WC_Product $product The product object.
* @return string The filtered sale badge text.
*/
$sale_text = apply_filters( 'woocommerce_sale_badge_text', __( 'Sale', 'woocommerce' ), $product );
$output = '<div class="wp-block-woocommerce-product-sale-badge ' . esc_attr( $classname ) . '">';
$output .= sprintf( '<div class="wc-block-components-product-sale-badge %1$s wc-block-components-product-sale-badge--align-%2$s" style="%3$s">', esc_attr( $classes_and_styles['classes'] ), esc_attr( $align ), esc_attr( $classes_and_styles['styles'] ) );
$output .= '<span class="wc-block-components-product-sale-badge__text" aria-hidden="true">' . esc_html( $sale_text ) . '</span>';
$output .= '<span class="screen-reader-text">'
. __( 'Product on sale', 'woocommerce' )
. '</span>';
$output .= '</div></div>';
return $output;
}