Automattic\WooCommerce\Blocks\BlockTypes

CartOrderSummaryBlock::render()protectedWC 1.0

Render the Cart Order Summary block.

Method of the class: CartOrderSummaryBlock{}

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.

CartOrderSummaryBlock::render() code WC 8.9.0

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_cart_order_summary_totals = '/<div data-block-name="woocommerce\/cart-order-summary-totals-block"(.+?)>/';
	$order_summary_totals_content        = '<div data-block-name="woocommerce/cart-order-summary-totals-block" class="wp-block-woocommerce-cart-order-summary-totals-block">';

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

	if ( preg_match( $regex_for_cart_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 );
			}
		}
	}

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