Force method/pickup in the checkout layout (https://github.com/woocommerce/woocommerce-blocks/pull/7910)
This commit is contained in:
parent
cb830f5ec8
commit
d9fbaa5ec3
|
@ -122,7 +122,62 @@ export const useForcedLayout = ( {
|
|||
.select( 'core/block-editor' )
|
||||
.getBlocks( clientId );
|
||||
|
||||
// If there are NO inner blocks, sync with the given template.
|
||||
const { innerBlocks, registeredBlockTypes } = useSelect(
|
||||
( select ) => {
|
||||
return {
|
||||
innerBlocks:
|
||||
select( 'core/block-editor' ).getBlocks( clientId ),
|
||||
registeredBlockTypes: currentRegisteredBlocks.current.map(
|
||||
( blockName ) => getBlockType( blockName )
|
||||
),
|
||||
};
|
||||
},
|
||||
[ clientId, currentRegisteredBlocks.current ]
|
||||
);
|
||||
|
||||
const appendBlock = useCallback(
|
||||
( block, position ) => {
|
||||
const newBlock = createBlock( block.name );
|
||||
insertBlock( newBlock, position, clientId, false );
|
||||
},
|
||||
// We need to skip insertBlock here due to a cache issue in wordpress.com that causes an infinite loop, see https://github.com/Automattic/wp-calypso/issues/66092 for an expanded doc.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[ clientId ]
|
||||
);
|
||||
|
||||
const lockedBlockTypes = useMemo(
|
||||
() =>
|
||||
registeredBlockTypes.filter(
|
||||
( block: Block | undefined ) => block && isBlockLocked( block )
|
||||
),
|
||||
[ registeredBlockTypes ]
|
||||
) as Block[];
|
||||
|
||||
/**
|
||||
* If the current inner blocks differ from the registered blocks, push the differences.
|
||||
*/
|
||||
useLayoutEffect( () => {
|
||||
if ( ! clientId ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If there are NO inner blocks, sync with the given template.
|
||||
if (
|
||||
innerBlocks.length === 0 &&
|
||||
currentDefaultTemplate.current.length > 0
|
||||
) {
|
||||
const nextBlocks = createBlocksFromInnerBlocksTemplate(
|
||||
currentDefaultTemplate.current
|
||||
);
|
||||
if ( ! isEqual( nextBlocks, innerBlocks ) ) {
|
||||
replaceInnerBlocks( clientId, nextBlocks );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Find registered locked blocks missing from Inner Blocks and append them.
|
||||
lockedBlockTypes.forEach( ( block ) => {
|
||||
// If the locked block type is already in the layout, we can skip this one.
|
||||
if (
|
||||
innerBlocks.length === 0 &&
|
||||
currentDefaultTemplate.current.length > 0
|
||||
|
|
|
@ -146,6 +146,18 @@ class Checkout extends AbstractBlock {
|
|||
$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[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-shipping-method-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*>/mi';
|
||||
$has_local_pickup = preg_match( $has_local_pickup_regex, $content );
|
||||
|
||||
if ( ! $has_local_pickup ) {
|
||||
$shipping_address_block_regex = '/<div[\n\r\s\ta-zA-Z0-9_\-=\'"]*data-block-name="woocommerce\/checkout-shipping-address-block" class="wp-block-woocommerce-checkout-shipping-address-block"[\n\r\s\ta-zA-Z0-9_\-=\'"]*><\/div>/mi';
|
||||
$content = preg_replace( $shipping_address_block_regex, $local_pickup_inner_blocks, $content );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue