Automattic\WooCommerce\Blocks\BlockTypes

AddToCartForm::renderprotectedWC 1.0

Render the block.

Method of the class: AddToCartForm{}

Returns

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 10.3.6

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

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

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

	$is_descendent_of_single_product_block = is_null( $product ) || $post_id !== $product->get_id();

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

		return '';
	}

	// Check if all attributes are set for variation product.
	if ( $product->is_type( ProductType::VARIATION ) && ! $this->has_all_attributes_set( $product ) ) {
		$product = $previous_product;

		return '';
	}

	$is_external_product_with_url = $product instanceof \WC_Product_External && $product->get_product_url();
	$managing_stock               = $product->managing_stock();
	$stock_quantity               = $product->get_stock_quantity();

	$should_hide_quantity_selector = $product->is_sold_individually() || Utils::is_min_max_quantity_same( $product ) || ( $managing_stock && $stock_quantity <= 1 );

	/**
	 * The stepper buttons don't show when the product is sold individually or stock quantity is less or equal to 1 because the quantity input field is hidden.
	 * Additionally, if min and max purchase quantity are the same, the buttons should not be rendered at all.
	 */
	$is_stepper_style = 'stepper' === $attributes['quantitySelectorStyle'] && ! $should_hide_quantity_selector;

	if ( $is_descendent_of_single_product_block ) {
		add_filter( 'woocommerce_add_to_cart_form_action', array( $this, 'add_to_cart_form_action' ), 10 );
	}

	ob_start();

	/**
	 * Manage variations in the same way as simple products.
	 */
	add_action( 'woocommerce_variation_add_to_cart', 'woocommerce_simple_add_to_cart', 10 );

	/**
	 * 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' );

	/**
	 * Remove the hook to prevent potential conflicts with existing code and extensions.
	 */
	remove_action( 'woocommerce_variation_add_to_cart', 'woocommerce_simple_add_to_cart', 10 );

	$product_html = ob_get_clean();

	if ( $is_descendent_of_single_product_block ) {
		remove_filter( 'woocommerce_add_to_cart_form_action', array( $this, 'add_to_cart_form_action' ), 10 );
	}

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

		return '';
	}

	$product_name = $product->get_name();
	$product_html = $is_stepper_style ? $this->add_steppers( $product_html, $product_name ) : $product_html;

	$product_html       = $is_stepper_style ? $this->add_stepper_classes_to_add_to_cart_form_input( $product_html ) : $product_html;
	$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'extra_classes' ) );

	$product_classname = $is_descendent_of_single_product_block ? 'product' : '';

	$classes = implode(
		' ',
		array_filter(
			array(
				'wp-block-add-to-cart-form wc-block-add-to-cart-form',
				esc_attr( $classes_and_styles['classes'] ),
				esc_attr( $product_classname ),
				$is_stepper_style ? 'wc-block-add-to-cart-form--stepper' : 'wc-block-add-to-cart-form--input',
			)
		)
	);

	$wrapper_attributes = get_block_wrapper_attributes(
		array(
			'class' => $classes,
			'style' => esc_attr( $classes_and_styles['styles'] ),
		)
	);

	$form = sprintf(
		'<div %1$s %2$s>%3$s</div>',
		$wrapper_attributes,
		$is_stepper_style ? 'data-wp-interactive="woocommerce/add-to-cart-form"' : '',
		$product_html
	);

	$product = $previous_product;

	return $form;
}