Automattic\WooCommerce\Blocks\BlockTypes

AddToCartWithOptionsQuantitySelector::render()protectedWC 1.0

Render the block.

Method of the class: AddToCartWithOptionsQuantitySelector{}

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.

AddToCartWithOptionsQuantitySelector::render() code WC 9.6.1

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

	$is_external_product_with_url        = $product instanceof \WC_Product_External && $product->get_product_url();
	$can_only_be_purchased_one_at_a_time = $product->is_sold_individually();

	if ( $is_external_product_with_url || $can_only_be_purchased_one_at_a_time ) {
		return '';
	}

	$is_stepper_style = 'stepper' === $attributes['quantitySelectorStyle'] && ! $product->is_sold_individually();

	ob_start();

	/**
	 * Hook: woocommerce_before_add_to_cart_quantity.
	 *
	 * Action that fires before the quantity input field is rendered.
	 *
	 * @since 2.7.0
	 */
	do_action( 'woocommerce_before_add_to_cart_quantity' );

	woocommerce_quantity_input(
		array(
			/**
			 * Filter the minimum quantity value allowed for the product.
			 *
			 * @since 2.0.0
			 * @param int        $min_value Minimum quantity value.
			 * @param WC_Product $product   Product object.
			 */
			'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
			/**
			 * Filter the maximum quantity value allowed for the product.
			 *
			 * @since 2.0.0
			 * @param int        $max_value Maximum quantity value.
			 * @param WC_Product $product   Product object.
			 */
			'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
			'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // phpcs:ignore WordPress.Security.NonceVerification.Missing
		)
	);

	/**
	 * Hook: woocommerce_after_add_to_cart_quantity.
	 *
	 * Action that fires after the quantity input field is rendered.
	 *
	 * @since 2.7.0
	 */
	do_action( 'woocommerce_after_add_to_cart_quantity' );

	$product_html = ob_get_clean();

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

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

	$classes = implode(
		' ',
		array_filter(
			array(
				'wp-block-add-to-cart-with-options-quantity-selector wc-block-add-to-cart-with-options__quantity-selector',
				esc_attr( $classes_and_styles['classes'] ),
				$is_stepper_style ? 'wc-block-add-to-cart-with-options__quantity-selector--stepper' : 'wc-block-add-to-cart-with-options__quantity-selector--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-wc-interactive=\'' . wp_json_encode(
			array(
				'namespace' => 'woocommerce/add-to-cart-with-options',
			),
			JSON_NUMERIC_CHECK | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
		) . '\'' : '',
		$product_html
	);

	$product = $previous_product;

	return $form;
}