Automattic\WooCommerce\Blocks\BlockTypes

AddToCartForm::render()protectedWC 1.0

Render the block.

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

Хуки из метода

Возвращает

Строку. | void Rendered block output.

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

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

Код AddToCartForm::render() WC 8.9.0

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;
}