Automattic\WooCommerce\Blocks\BlockTypes

ProductStockIndicator::render()protectedWC 1.0

Include and render the block.

Метод класса: ProductStockIndicator{}

Хуков нет.

Возвращает

Строку. Rendered block type output.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->render( $attributes, $content, $block );
$attributes(массив) (обязательный)
Block attributes.
По умолчанию: empty array
$content(строка) (обязательный)
Block content.
По умолчанию: empty string
$block(WP_Block) (обязательный)
Block instance.

Код ProductStockIndicator::render() WC 8.7.0

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 = $block->context['postId'];
	$product = wc_get_product( $post_id );

	if ( ! $product ) {
		return '';
	}

	$is_in_stock     = $product->is_in_stock();
	$is_on_backorder = $product->is_on_backorder();

	$low_stock_amount = $product->get_low_stock_amount();
	$total_stock      = $product->get_stock_quantity();
	$is_low_stock     = $low_stock_amount && $total_stock <= $low_stock_amount;

	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

	$classnames  = isset( $classes_and_styles['classes'] ) ? ' ' . $classes_and_styles['classes'] . ' ' : '';
	$classnames .= isset( $attributes['className'] ) ? ' ' . $attributes['className'] . ' ' : '';
	$classnames .= ! $is_in_stock ? ' wc-block-components-product-stock-indicator--out-of-stock ' : '';
	$classnames .= $is_in_stock ? ' wc-block-components-product-stock-indicator--in-stock ' : '';
	$classnames .= $is_low_stock ? ' wc-block-components-product-stock-indicator--low-stock ' : '';
	$classnames .= $is_on_backorder ? ' wc-block-components-product-stock-indicator--available-on-backorder ' : '';

	$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( self::getTextBasedOnStock( $is_in_stock, $is_low_stock, $low_stock_amount, $is_on_backorder ) );
	$output .= '</div>';

	return $output;

}