Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
GroupedProductItemLabel{}└─ AbstractBlock
Block type for the label of grouped product selector items in Add to Cart + Options. It's responsible to render the label for each child product.
No Hooks.
Usage
$GroupedProductItemLabel = new GroupedProductItemLabel(); // use class methods
Methods
- protected render( $attributes, $content, $block )
GroupedProductItemLabel{} GroupedProductItemLabel{} code WC 10.8.1
class GroupedProductItemLabel extends AbstractBlock {
use EnableBlockJsonAssetsTrait;
/**
* Block name.
*
* @var string
*/
protected $block_name = 'add-to-cart-with-options-grouped-product-item-label';
/**
* Render the block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param WP_Block $block Block instance.
* @return string Rendered block output.
*/
protected function render( $attributes, $content, $block ): string {
$product = AddToCartWithOptionsUtils::get_product_from_context( $block, $GLOBALS['product'] );
$markup = '';
if ( $product ) {
$wrapper_attributes = get_block_wrapper_attributes();
$title = $product->get_name();
if ( ! $product->is_purchasable() || $product->has_options() || ! $product->is_in_stock() ) {
$markup = sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
esc_html( $title )
);
} else {
// Checkbox.
$markup = sprintf(
'<label %1$s for="%2$s">%3$s</label>',
$wrapper_attributes,
esc_attr( 'quantity_' . $product->get_id() ),
esc_html( $title )
);
}
}
return $markup;
}
}