Automattic\WooCommerce\Blocks\BlockTypes

ProductStockIndicator::render()protectedWC 1.0

Include and render the block.

Method of the class: ProductStockIndicator{}

No Hooks.

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.

ProductStockIndicator::render() code WC 9.8.4

protected function render( $attributes, $content, $block ) {
	if ( ! empty( $content ) ) {
		parent::register_block_type_assets();
		$this->register_chunk_translations( [ $this->block_name ] );
		return $content;
	}
	$post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : '';
	$product = wc_get_product( $post_id );

	if ( ! $product || in_array( $product->get_type(), $this->get_product_types_without_stock_indicator(), true ) ) {
		return '';
	}

	$availability = ProductAvailabilityUtils::get_product_availability( $product );

	if ( empty( $availability['availability'] ) ) {
		return '';
	}

	$total_stock        = $product->get_stock_quantity();
	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

	$classnames  = isset( $classes_and_styles['classes'] ) ? ' ' . $classes_and_styles['classes'] . ' ' : '';
	$classnames .= sprintf( ' wc-block-components-product-stock-indicator--%s', $availability['class'] );

	$is_backorder_notification_visible = $product->is_in_stock() && $product->backorders_require_notification();

	if ( empty( $content ) && $is_backorder_notification_visible && $total_stock > 0 ) {
		$low_stock_text = sprintf(
			/* translators: %d is number of items in stock for product */
			__( '%d left in stock', 'woocommerce' ),
			$total_stock
		);
	}

	$output_text = $low_stock_text ?? $availability['availability'];

	$output  = '';
	$output .= '<div class="wc-block-components-product-stock-indicator wp-block-woocommerce-product-stock-indicator ' . esc_attr( $classnames ) . '"';
	$output .= isset( $classes_and_styles['styles'] ) ? ' style="' . esc_attr( $classes_and_styles['styles'] ) . '"' : '';
	$output .= '>';
	$output .= wp_kses_post( $output_text );
	$output .= '</div>';

	return $output;
}