Automattic\WooCommerce\Blocks\BlockTypes
ProductStockIndicator::render
Renders the stock indicator block.
This method handles both direct product context and global product context, ensuring the stock indicator displays correctly in various template scenarios.
Метод класса: 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() ProductStockIndicator::render WC 11.0.0
protected function render( $attributes, $content, $block ) {
global $product;
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_to_render = wc_get_product( $post_id );
// Use the global product if the product to render can't be retrieved from the context.
if ( ! $product_to_render instanceof WC_Product ) {
$product_to_render = $product;
}
if ( ! $product_to_render || in_array( $product_to_render->get_type(), $this->get_product_types_without_stock_indicator(), true ) ) {
return '';
}
$availability = ProductAvailabilityUtils::get_product_availability( $product_to_render );
$is_descendant_of_product_collection = isset( $block->context['query']['isProductCollectionBlock'] );
$is_descendant_of_grouped_product_selector = isset( $block->context['isDescendantOfGroupedProductSelector'] );
$is_interactive = ! $is_descendant_of_product_collection && ! $is_descendant_of_grouped_product_selector && $product_to_render->is_type( ProductType::VARIABLE );
if ( empty( $availability['availability'] ) && ! $is_interactive ) {
return '';
}
$total_stock = $product_to_render->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_to_render->is_in_stock() && $product_to_render->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
);
}
$wrapper_attributes = array();
$watch_attribute = '';
if ( $is_interactive && 'out-of-stock' !== $availability['class'] ) {
wp_enqueue_script_module( 'woocommerce/product-elements' );
$wrapper_attributes['data-wp-interactive'] = 'woocommerce/products';
$wrapper_attributes['data-wp-text'] = 'state.productInContext.stock_availability.text';
$wrapper_attributes['aria-live'] = 'polite';
$wrapper_attributes['aria-atomic'] = 'true';
}
$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'] ) . '"' : '';
if ( $is_interactive && 'out-of-stock' !== $availability['class'] ) {
$output .= ' ' . get_block_wrapper_attributes( $wrapper_attributes );
}
$output .= '>';
$output .= wp_kses_post( $output_text );
$output .= '</div>';
return $output;
}