Automattic\WooCommerce\Blocks\BlockTypes

Checkout::render()protectedWC 1.0

Append frontend scripts when rendering the block.

Method of the class: Checkout{}

No Hooks.

Return

String. Rendered block type 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.

Checkout::render() code WC 8.9.0

protected function render( $attributes, $content, $block ) {

	if ( $this->is_checkout_endpoint() ) {
		// Note: Currently the block only takes care of the main checkout form -- if an endpoint is set, refer to the
		// legacy shortcode instead and do not render block.
		return wc_current_theme_is_fse_theme() ? do_shortcode( '[woocommerce_checkout]' ) : '[woocommerce_checkout]';
	}

	// Dequeue the core scripts when rendering this block.
	add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_woocommerce_core_scripts' ), 20 );

	/**
	 * We need to check if $content has any templates from prior iterations of the block, in order to update to the latest iteration.
	 * We test the iteration version by searching for new blocks brought in by it.
	 * The blocks used for testing should be always available in the block (not removable by the user).
	 * Checkout i1's content was returning an empty div, with no data-block-name attribute
	 */
	$regex_for_empty_block = '/<div class="[a-zA-Z0-9_\- ]*wp-block-woocommerce-checkout[a-zA-Z0-9_\- ]*"><\/div>/mi';
	$has_i1_template       = preg_match( $regex_for_empty_block, $content );

	if ( $has_i1_template ) {
		// This fallback needs to match the default templates defined in our Blocks.
		$inner_blocks_html = '
			<div data-block-name="woocommerce/checkout-fields-block" class="wp-block-woocommerce-checkout-fields-block">
				<div data-block-name="woocommerce/checkout-express-payment-block" class="wp-block-woocommerce-checkout-express-payment-block"></div>
				<div data-block-name="woocommerce/checkout-contact-information-block" class="wp-block-woocommerce-checkout-contact-information-block"></div>
				<div data-block-name="woocommerce/checkout-shipping-address-block" class="wp-block-woocommerce-checkout-shipping-address-block"></div>
				<div data-block-name="woocommerce/checkout-billing-address-block" class="wp-block-woocommerce-checkout-billing-address-block"></div>
				<div data-block-name="woocommerce/checkout-shipping-methods-block" class="wp-block-woocommerce-checkout-shipping-methods-block"></div>
				<div data-block-name="woocommerce/checkout-payment-block" class="wp-block-woocommerce-checkout-payment-block"></div>
				<div data-block-name="woocommerce/checkout-additional-information-block" class="wp-block-woocommerce-checkout-additional-information-block"></div>' .
				( isset( $attributes['showOrderNotes'] ) && false === $attributes['showOrderNotes'] ? '' : '<div data-block-name="woocommerce/checkout-order-note-block" class="wp-block-woocommerce-checkout-order-note-block"></div>' ) .
				( isset( $attributes['showPolicyLinks'] ) && false === $attributes['showPolicyLinks'] ? '' : '<div data-block-name="woocommerce/checkout-terms-block" class="wp-block-woocommerce-checkout-terms-block"></div>' ) .
				'<div data-block-name="woocommerce/checkout-actions-block" class="wp-block-woocommerce-checkout-actions-block"></div>
			</div>
			<div data-block-name="woocommerce/checkout-totals-block" class="wp-block-woocommerce-checkout-totals-block">
				<div data-block-name="woocommerce/checkout-order-summary-block" class="wp-block-woocommerce-checkout-order-summary-block"></div>
			</div>
		';

		$content = str_replace( '</div>', $inner_blocks_html . '</div>', $content );
	}

	/**
	 * Checkout i3 added inner blocks for Order summary.
	 * We need to add them to Checkout i2 templates.
	 * The order needs to match the order in which these blocks were registered.
	 */
	$order_summary_with_inner_blocks = '$0
		<div data-block-name="woocommerce/checkout-order-summary-cart-items-block" class="wp-block-woocommerce-checkout-order-summary-cart-items-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-subtotal-block" class="wp-block-woocommerce-checkout-order-summary-subtotal-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-fee-block" class="wp-block-woocommerce-checkout-order-summary-fee-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-discount-block" class="wp-block-woocommerce-checkout-order-summary-discount-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-coupon-form-block" class="wp-block-woocommerce-checkout-order-summary-coupon-form-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-shipping-block" class="wp-block-woocommerce-checkout-order-summary-shipping-block"></div>
		<div data-block-name="woocommerce/checkout-order-summary-taxes-block" class="wp-block-woocommerce-checkout-order-summary-taxes-block"></div>
	';
	// Order summary subtotal block was added in i3, so we search for it to see if we have a Checkout i2 template.
	$regex_for_order_summary_subtotal = '/<div[^<]*?data-block-name="woocommerce\/checkout-order-summary-subtotal-block"[^>]*?>/mi';
	$regex_for_order_summary          = '/<div[^<]*?data-block-name="woocommerce\/checkout-order-summary-block"[^>]*?>/mi';
	$has_i2_template                  = ! preg_match( $regex_for_order_summary_subtotal, $content );

	if ( $has_i2_template ) {
		$content = preg_replace( $regex_for_order_summary, $order_summary_with_inner_blocks, $content );
	}

	/**
	 * Add the Local Pickup toggle to checkouts missing this forced template.
	 */
	$local_pickup_inner_blocks = '<div data-block-name="woocommerce/checkout-shipping-method-block" class="wp-block-woocommerce-checkout-shipping-method-block"></div>' . PHP_EOL . PHP_EOL . '<div data-block-name="woocommerce/checkout-pickup-options-block" class="wp-block-woocommerce-checkout-pickup-options-block"></div>' . PHP_EOL . PHP_EOL . '$0';
	$has_local_pickup_regex    = '/<div[^<]*?data-block-name="woocommerce\/checkout-shipping-method-block"[^>]*?>/mi';
	$has_local_pickup          = preg_match( $has_local_pickup_regex, $content );

	if ( ! $has_local_pickup ) {
		$shipping_address_block_regex = '/<div[^<]*?data-block-name="woocommerce\/checkout-shipping-address-block" class="wp-block-woocommerce-checkout-shipping-address-block"[^>]*?><\/div>/mi';
		$content                      = preg_replace( $shipping_address_block_regex, $local_pickup_inner_blocks, $content );
	}

	/**
	 * Add the Additional Information block to checkouts missing it.
	 */
	$additional_information_inner_blocks = '$0' . PHP_EOL . PHP_EOL . '<div data-block-name="woocommerce/checkout-additional-information-block" class="wp-block-woocommerce-checkout-additional-information-block"></div>' . PHP_EOL . PHP_EOL;
	$has_additional_information_regex    = '/<div[^<]*?data-block-name="woocommerce\/checkout-additional-information-block"[^>]*?>/mi';
	$has_additional_information_block    = preg_match( $has_additional_information_regex, $content );

	if ( ! $has_additional_information_block ) {
		$payment_block_regex = '/<div[^<]*?data-block-name="woocommerce\/checkout-payment-block" class="wp-block-woocommerce-checkout-payment-block"[^>]*?><\/div>/mi';
		$content             = preg_replace( $payment_block_regex, $additional_information_inner_blocks, $content );
	}

	return $content;
}