Refactor the Order Summary of the Cart & Checkout blocks (#46386)

* Added new OrderSummaryTotals Block

* Refactor order summary and totals block to contain the inner children

* Add migration for Order Summary Block

* Migration to handle older order summary without totals block

* Remove empty lines from order summary migration

* block editor migration for order summary

* Fix isEligible function

* Update Totals title

* Add checkout totals

* Remove echos and dies

* replace individual checkout blocks with totals block

* Change parent of totals inner blocks and add totals block to innerBlockAreas map

* change CART_ORDER_SUMMARY_TOTALS to CHECKOUT_ORDER_SUMMARY_TOTALS

* Fix linting issues

* fix lint warnings

* Add  changelog

* Lock movement of the taxes block in attributes.tsx

* Less indentation in the render function of CartOrderSummaryBlock and CheckoutOrderSummaryBlock

* Rename webpack chunkName for order summary totals
This commit is contained in:
Alex Florisca 2024-04-17 10:28:30 +01:00 committed by GitHub
parent 87868097d2
commit 3089b1888e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
38 changed files with 608 additions and 31 deletions

View File

@ -0,0 +1,83 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import metadata from './block.json';
const deprecated = [
{
attributes: metadata.attributes,
save: () => {
return (
<div { ...useBlockProps.save() }>
<InnerBlocks.Content />
</div>
);
},
supports: metadata.supports,
migrate: ( { attributes } ) => {
return [
attributes,
[
createBlock(
'woocommerce/cart-order-summary-heading-block',
{
content: __( 'Cart totals', 'woocommerce' ),
},
[]
),
createBlock(
'woocommerce/cart-order-summary-coupon-form-block',
{},
[]
),
createBlock(
'woocommerce/cart-order-summary-totals-block',
{},
[
createBlock(
'woocommerce/cart-order-summary-subtotal-block',
{},
[]
),
createBlock(
'woocommerce/cart-order-summary-fee-block',
{},
[]
),
createBlock(
'woocommerce/cart-order-summary-discount-block',
{},
[]
),
createBlock(
'woocommerce/cart-order-summary-shipping-block',
{},
[]
),
createBlock(
'woocommerce/cart-order-summary-taxes-block',
{},
[]
),
]
),
],
];
},
isEligible: ( attributes, innerBlocks ) => {
return ! innerBlocks.some(
( block ) =>
block.name === 'woocommerce/cart-order-summary-totals-block'
);
},
},
];
export default deprecated;

View File

@ -34,11 +34,7 @@ export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
[],
],
[ 'woocommerce/cart-order-summary-coupon-form-block', {}, [] ],
[ 'woocommerce/cart-order-summary-subtotal-block', {}, [] ],
[ 'woocommerce/cart-order-summary-fee-block', {}, [] ],
[ 'woocommerce/cart-order-summary-discount-block', {}, [] ],
[ 'woocommerce/cart-order-summary-shipping-block', {}, [] ],
[ 'woocommerce/cart-order-summary-taxes-block', {}, [] ],
[ 'woocommerce/cart-order-summary-totals-block', {}, [] ],
] as TemplateArray;
useForcedLayout( {

View File

@ -9,6 +9,7 @@ import { registerBlockType } from '@wordpress/blocks';
* Internal dependencies
*/
import { Edit, Save } from './edit';
import deprecated from './deprecated';
registerBlockType( 'woocommerce/cart-order-summary-block', {
icon: {
@ -21,4 +22,5 @@ registerBlockType( 'woocommerce/cart-order-summary-block', {
},
edit: Edit,
save: Save,
deprecated,
} );

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"parent": [ "woocommerce/cart-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"parent": [ "woocommerce/cart-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -16,11 +16,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"parent": [ "woocommerce/cart-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"parent": [ "woocommerce/cart-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"parent": [ "woocommerce/cart-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -0,0 +1,31 @@
{
"name": "woocommerce/cart-order-summary-totals-block",
"version": "1.0.0",
"title": "Totals",
"description": "Shows the subtotal, fees, discounts, shipping and taxes.",
"category": "woocommerce",
"supports": {
"align": false,
"html": false,
"multiple": false,
"reusable": false,
"lock": false
},
"attributes": {
"className": {
"type": "string",
"default": ""
},
"lock": {
"type": "object",
"default": {
"remove": true,
"move": false
}
}
},
"parent": [ "woocommerce/cart-order-summary-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2
}

View File

@ -0,0 +1,51 @@
/**
* External dependencies
*/
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
import type { TemplateArray } from '@wordpress/blocks';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import {
useForcedLayout,
getAllowedBlocks,
} from '../../../cart-checkout-shared';
export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
const blockProps = useBlockProps();
const allowedBlocks = getAllowedBlocks(
innerBlockAreas.CART_ORDER_SUMMARY_TOTALS
);
const defaultTemplate = [
[ 'woocommerce/cart-order-summary-subtotal-block', {}, [] ],
[ 'woocommerce/cart-order-summary-fee-block', {}, [] ],
[ 'woocommerce/cart-order-summary-discount-block', {}, [] ],
[ 'woocommerce/cart-order-summary-shipping-block', {}, [] ],
[ 'woocommerce/cart-order-summary-taxes-block', {}, [] ],
] as TemplateArray;
useForcedLayout( {
clientId,
registeredBlocks: allowedBlocks,
defaultTemplate,
} );
return (
<div { ...blockProps }>
<InnerBlocks
allowedBlocks={ allowedBlocks }
template={ defaultTemplate }
/>
</div>
);
};
export const Save = (): JSX.Element => {
return (
<div { ...useBlockProps.save() }>
<InnerBlocks.Content />
</div>
);
};

View File

@ -0,0 +1,11 @@
const FrontendBlock = ( {
children,
className = '',
}: {
children?: JSX.Element | JSX.Element[];
className?: string;
} ): JSX.Element | null => {
return <div className={ className }>{ children }</div>;
};
export default FrontendBlock;

View File

@ -0,0 +1,24 @@
/**
* External dependencies
*/
import { totals } from '@woocommerce/icons';
import { Icon } from '@wordpress/icons';
import { registerBlockType } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { Edit, Save } from './edit';
registerBlockType( 'woocommerce/cart-order-summary-totals-block', {
icon: {
src: (
<Icon
icon={ totals }
className="wc-block-editor-components-block-icon"
/>
),
},
edit: Edit,
save: Save,
} );

View File

@ -15,6 +15,7 @@ import CART_ORDER_SUMMARY from './cart-order-summary-block/block.json';
import CART_ORDER_SUMMARY_SUBTOTAL from './cart-order-summary-subtotal/block.json';
import CART_ORDER_SUMMARY_FEE from './cart-order-summary-fee/block.json';
import CART_ORDER_SUMMARY_DISCOUNT from './cart-order-summary-discount/block.json';
import CART_ORDER_SUMMARY_TOTALS from './cart-order-summary-totals/block.json';
import CART_ORDER_SUMMARY_SHIPPING from './cart-order-summary-shipping/block.json';
import CART_ORDER_SUMMARY_COUPON_FORM from './cart-order-summary-coupon-form/block.json';
import CART_ORDER_SUMMARY_TAXES from './cart-order-summary-taxes/block.json';
@ -38,5 +39,6 @@ export default {
CART_ORDER_SUMMARY_SHIPPING,
CART_ORDER_SUMMARY_COUPON_FORM,
CART_ORDER_SUMMARY_TAXES,
CART_ORDER_SUMMARY_TOTALS,
CART_ORDER_SUMMARY_HEADING,
};

View File

@ -13,6 +13,7 @@ import './empty-cart-block';
import './cart-accepted-payment-methods-block';
import './cart-order-summary-block';
import './cart-order-summary-subtotal';
import './cart-order-summary-totals';
import './cart-order-summary-fee';
import './cart-order-summary-discount';
import './cart-order-summary-shipping';

View File

@ -201,6 +201,17 @@ registerCheckoutBlock( {
),
} );
registerCheckoutBlock( {
metadata: metadata.CART_ORDER_SUMMARY_TOTALS,
component: lazy(
() =>
import(
/* webpackChunkName: "cart-blocks/order-summary-totals" */
'./cart-order-summary-totals/frontend'
)
),
} );
registerCheckoutBlock( {
metadata: metadata.CART_ORDER_SUMMARY_TAXES,
component: lazy(

View File

@ -0,0 +1,81 @@
/**
* External dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import metadata from './block.json';
const deprecated = [
{
attributes: metadata.attributes,
save: () => {
return (
<div { ...useBlockProps.save() }>
<InnerBlocks.Content />
</div>
);
},
supports: metadata.supports,
migrate: ( { attributes } ) => {
return [
attributes,
[
createBlock(
'woocommerce/checkout-order-summary-cart-items-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-coupon-form-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-totals-block',
{},
[
createBlock(
'woocommerce/checkout-order-summary-subtotal-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-fee-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-discount-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-shipping-block',
{},
[]
),
createBlock(
'woocommerce/checkout-order-summary-taxes-block',
{},
[]
),
]
),
],
];
},
isEligible: ( attributes, innerBlocks ) => {
return ! innerBlocks.some(
( block ) =>
block.name ===
'woocommerce/checkout-order-summary-totals-block'
);
},
},
];
export default deprecated;

View File

@ -27,11 +27,7 @@ export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
const defaultTemplate = [
[ 'woocommerce/checkout-order-summary-cart-items-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-coupon-form-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-subtotal-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-fee-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-discount-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-shipping-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-taxes-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-totals-block', {}, [] ],
] as TemplateArray;
useForcedLayout( {

View File

@ -10,6 +10,7 @@ import { registerBlockType } from '@wordpress/blocks';
*/
import { Edit, Save } from './edit';
import attributes from './attributes';
import deprecated from './deprecated';
registerBlockType( 'woocommerce/checkout-order-summary-block', {
icon: {
@ -23,4 +24,5 @@ registerBlockType( 'woocommerce/checkout-order-summary-block', {
attributes,
edit: Edit,
save: Save,
deprecated,
} );

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"parent": [ "woocommerce/checkout-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"parent": [ "woocommerce/checkout-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -16,11 +16,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"parent": [ "woocommerce/checkout-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"parent": [ "woocommerce/checkout-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -12,7 +12,7 @@ export default {
type: 'object',
default: {
remove: true,
move: false,
move: true,
},
},
};

View File

@ -20,11 +20,11 @@
"type": "object",
"default": {
"remove": true,
"move": false
"move": true
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"parent": [ "woocommerce/checkout-order-summary-totals-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2

View File

@ -0,0 +1,31 @@
{
"name": "woocommerce/checkout-order-summary-totals-block",
"version": "1.0.0",
"title": "Totals",
"description": "Shows the subtotal, fees, discounts, shipping and taxes.",
"category": "woocommerce",
"supports": {
"align": false,
"html": false,
"multiple": false,
"reusable": false,
"lock": false
},
"attributes": {
"className": {
"type": "string",
"default": ""
},
"lock": {
"type": "object",
"default": {
"remove": true,
"move": false
}
}
},
"parent": [ "woocommerce/checkout-order-summary-block" ],
"textdomain": "woocommerce",
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2
}

View File

@ -0,0 +1,51 @@
/**
* External dependencies
*/
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';
import type { TemplateArray } from '@wordpress/blocks';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import {
useForcedLayout,
getAllowedBlocks,
} from '../../../cart-checkout-shared';
export const Edit = ( { clientId }: { clientId: string } ): JSX.Element => {
const blockProps = useBlockProps();
const allowedBlocks = getAllowedBlocks(
innerBlockAreas.CHECKOUT_ORDER_SUMMARY_TOTALS
);
const defaultTemplate = [
[ 'woocommerce/checkout-order-summary-subtotal-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-fee-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-discount-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-shipping-block', {}, [] ],
[ 'woocommerce/checkout-order-summary-taxes-block', {}, [] ],
] as TemplateArray;
useForcedLayout( {
clientId,
registeredBlocks: allowedBlocks,
defaultTemplate,
} );
return (
<div { ...blockProps }>
<InnerBlocks
allowedBlocks={ allowedBlocks }
template={ defaultTemplate }
/>
</div>
);
};
export const Save = (): JSX.Element => {
return (
<div { ...useBlockProps.save() }>
<InnerBlocks.Content />
</div>
);
};

View File

@ -0,0 +1,11 @@
const FrontendBlock = ( {
children,
className = '',
}: {
children?: JSX.Element | JSX.Element[];
className?: string;
} ): JSX.Element | null => {
return <div className={ className }>{ children }</div>;
};
export default FrontendBlock;

View File

@ -0,0 +1,24 @@
/**
* External dependencies
*/
import { totals } from '@woocommerce/icons';
import { Icon } from '@wordpress/icons';
import { registerBlockType } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { Edit, Save } from './edit';
registerBlockType( 'woocommerce/checkout-order-summary-totals-block', {
icon: {
src: (
<Icon
icon={ totals }
className="wc-block-editor-components-block-icon"
/>
),
},
edit: Edit,
save: Save,
} );

View File

@ -23,6 +23,7 @@ import CHECKOUT_ORDER_SUMMARY_SHIPPING from './checkout-order-summary-shipping/b
import CHECKOUT_ORDER_SUMMARY_COUPON_FORM from './checkout-order-summary-coupon-form/block.json';
import CHECKOUT_ORDER_SUMMARY_TAXES from './checkout-order-summary-taxes/block.json';
import CHECKOUT_ORDER_SUMMARY_CART_ITEMS from './checkout-order-summary-cart-items/block.json';
import CHECKOUT_ORDER_SUMMARY_TOTALS from './checkout-order-summary-totals/block.json';
export default {
CHECKOUT_ACTIONS,
@ -47,4 +48,5 @@ export default {
CHECKOUT_ORDER_SUMMARY_COUPON_FORM,
CHECKOUT_ORDER_SUMMARY_TAXES,
CHECKOUT_ORDER_SUMMARY_CART_ITEMS,
CHECKOUT_ORDER_SUMMARY_TOTALS,
};

View File

@ -23,3 +23,4 @@ import './checkout-order-summary-shipping';
import './checkout-order-summary-coupon-form';
import './checkout-order-summary-taxes';
import './checkout-order-summary-cart-items';
import './checkout-order-summary-totals';

View File

@ -29,7 +29,9 @@ export enum innerBlockAreas {
MINI_CART_ITEMS = 'woocommerce/mini-cart-items-block',
MINI_CART_FOOTER = 'woocommerce/mini-cart-footer-block',
CART_ORDER_SUMMARY = 'woocommerce/cart-order-summary-block',
CART_ORDER_SUMMARY_TOTALS = 'woocommerce/cart-order-summary-totals-block',
CHECKOUT_ORDER_SUMMARY = 'woocommerce/checkout-order-summary-block',
CHECKOUT_ORDER_SUMMARY_TOTALS = 'woocommerce/checkout-order-summary-totals-block',
}
interface CheckoutBlockOptionsMetadata extends Partial< BlockConfiguration > {

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Group subtotals in the Order Summary of the Cart & Checkout blocks

View File

@ -285,6 +285,7 @@ class Cart extends AbstractBlock {
'Cart',
'CartOrderSummaryTaxesBlock',
'CartOrderSummarySubtotalBlock',
'CartOrderSummaryTotalsBlock',
'FilledCartBlock',
'EmptyCartBlock',
'CartTotalsBlock',

View File

@ -11,4 +11,68 @@ class CartOrderSummaryBlock extends AbstractInnerBlock {
* @var string
*/
protected $block_name = 'cart-order-summary-block';
/**
* Get the contents of the given inner block.
*
* @param string $block_name Name of the order summary inner block.
* @param string $content The content to search.
* @return array|bool
*/
private function get_inner_block_content( $block_name, $content ) {
if ( preg_match( $this->inner_block_regex( $block_name ), $content, $matches ) ) {
return $matches[0];
}
return false;
}
/**
* Get the regex that will return an inner block.
*
* @param string $block_name Name of the order summary inner block.
* @return string Regex pattern.
*/
private function inner_block_regex( $block_name ) {
return '/<div data-block-name="woocommerce\/cart-order-summary-' . $block_name . '-block"(.+?)>(.*?)<\/div>/si';
}
/**
* Render the Cart Order Summary block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param object $block Block object.
* @return string Rendered block.
*/
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 );
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;
/**
* CartOrderSummaryTotalsBlock class.
*/
class CartOrderSummaryTotalsBlock extends AbstractInnerBlock {
/**
* Block name.
*
* @var string
*/
protected $block_name = 'cart-order-summary-totals-block';
}

View File

@ -570,6 +570,7 @@ class Checkout extends AbstractBlock {
'CheckoutOrderSummaryShippingBlock',
'CheckoutOrderSummarySubtotalBlock',
'CheckoutOrderSummaryTaxesBlock',
'CheckoutOrderSummaryTotalsBlock',
'CheckoutPaymentBlock',
'CheckoutShippingAddressBlock',
'CheckoutShippingMethodsBlock',

View File

@ -11,4 +11,70 @@ class CheckoutOrderSummaryBlock extends AbstractInnerBlock {
* @var string
*/
protected $block_name = 'checkout-order-summary-block';
/**
* Get the contents of the given inner block.
*
* @param string $block_name Name of the order summary inner block.
* @param string $content The content to search.
* @return array|bool
*/
private function get_inner_block_content( $block_name, $content ) {
if ( preg_match( $this->inner_block_regex( $block_name ), $content, $matches ) ) {
return $matches[0];
}
return false;
}
/**
* Get the regex that will return an inner block.
*
* @param string $block_name Name of the order summary inner block.
* @return string Regex pattern.
*/
private function inner_block_regex( $block_name ) {
return '/<div data-block-name="woocommerce\/checkout-order-summary-' . $block_name . '-block"(.+?)>(.*?)<\/div>/si';
}
/**
* Render the Checkout Order Summary block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param object $block Block object.
* @return string Rendered block.
*/
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 );
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;
/**
* CheckoutOrderSummaryTotalsBlock class.
*/
class CheckoutOrderSummaryTotalsBlock extends AbstractInnerBlock {
/**
* Block name.
*
* @var string
*/
protected $block_name = 'checkout-order-summary-totals-block';
}