Automattic\WooCommerce\Blocks\BlockTypes

CheckoutOrderSummaryBlock::render()protectedWC 1.0

Render the Checkout Order Summary block.

Method of the class: CheckoutOrderSummaryBlock{}

No Hooks.

Return

String. Rendered block.

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(object) (required)
Block object.

CheckoutOrderSummaryBlock::render() code WC 9.4.2

protected function render( $attributes, $content, $block ) {
	// The order-summary-totals block was introduced as a new parent block for the totals
	// (subtotal, discount, fees, shipping and taxes) blocks.
	$regex_for_checkout_order_summary_totals = '/<div data-block-name="woocommerce\/checkout-order-summary-totals-block"(.+?)>/';
	$order_summary_totals_content            = '<div data-block-name="woocommerce/checkout-order-summary-totals-block" class="wp-block-woocommerce-checkout-order-summary-totals-block">';

	// We want to move these blocks inside a parent 'totals' block.
	$totals_inner_blocks = array( 'subtotal', 'discount', 'fee', 'shipping', 'taxes' );

	if ( preg_match( $regex_for_checkout_order_summary_totals, $content ) ) {
		return $content;
	}

	foreach ( $totals_inner_blocks as $key => $block_name ) {
		$inner_block_content = $this->get_inner_block_content( $block_name, $content );

		if ( $inner_block_content ) {
			$order_summary_totals_content .= "\n" . $inner_block_content;

			// The last block is replaced with the totals block.
			if ( count( $totals_inner_blocks ) - 1 === $key ) {
				$order_summary_totals_content .= '</div>';
				$content                       = preg_replace( $this->inner_block_regex( $block_name ), $order_summary_totals_content, $content );
			} else {
				// Otherwise, remove the block.
				$content = preg_replace( $this->inner_block_regex( $block_name ), '', $content );
			}
		}
	}

	// Remove empty lines.
	return preg_replace( '/\n\n( *?)/i', '', $content );
}