Automattic\WooCommerce\Blocks\BlockTypes

AddToCartForm::render()protectedWC 1.0

Render the block.

Method of the class: AddToCartForm{}

Return

String. | void Rendered block output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $attributes, $content, $block );
$attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

AddToCartForm::render() code WC 9.3.3

protected function render( $attributes, $content, $block ) {
	global $product;

	$post_id = $block->context['postId'];

	if ( ! isset( $post_id ) ) {
		return '';
	}

	$previous_product = $product;
	$product          = wc_get_product( $post_id );
	if ( ! $product instanceof \WC_Product ) {
		$product = $previous_product;

		return '';
	}

	ob_start();

	/**
	 * Trigger the single product add to cart action for each product type.
	*
	* @since 9.7.0
	*/
	do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );

	$product = ob_get_clean();

	if ( ! $product ) {
		$product = $previous_product;

		return '';
	}

	$parsed_attributes                     = $this->parse_attributes( $attributes );
	$is_descendent_of_single_product_block = $parsed_attributes['isDescendentOfSingleProductBlock'];
	$product                               = $this->add_is_descendent_of_single_product_block_hidden_input_to_product_form( $product, $is_descendent_of_single_product_block );

	$classname          = $attributes['className'] ?? '';
	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
	$product_classname  = $is_descendent_of_single_product_block ? 'product' : '';

	$form = sprintf(
		'<div class="wp-block-add-to-cart-form wc-block-add-to-cart-form %1$s %2$s %3$s" style="%4$s">%5$s</div>',
		esc_attr( $classes_and_styles['classes'] ),
		esc_attr( $classname ),
		esc_attr( $product_classname ),
		esc_attr( $classes_and_styles['styles'] ),
		$product
	);

	$product = $previous_product;

	return $form;
}