2023-09-19 09:58:18 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { createBlock, type BlockInstance } from '@wordpress/blocks';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import type { OnClickCallbackParameter, InheritedAttributes } from './types';
|
|
|
|
|
|
|
|
const isConversionPossible = () => {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2023-12-12 23:05:20 +00:00
|
|
|
const getButtonLabel = () => __( 'Transform into blocks', 'woocommerce' );
|
2023-09-19 09:58:18 +00:00
|
|
|
|
|
|
|
const getBlockifiedTemplate = ( inheritedAttributes: InheritedAttributes ) =>
|
|
|
|
[
|
|
|
|
createBlock( 'woocommerce/cart', {
|
|
|
|
...inheritedAttributes,
|
|
|
|
className: 'wc-block-cart',
|
|
|
|
} ),
|
|
|
|
].filter( Boolean ) as BlockInstance[];
|
|
|
|
|
|
|
|
const onClickCallback = ( {
|
|
|
|
clientId,
|
|
|
|
attributes,
|
|
|
|
getBlocks,
|
|
|
|
replaceBlock,
|
|
|
|
selectBlock,
|
|
|
|
}: OnClickCallbackParameter ) => {
|
|
|
|
replaceBlock( clientId, getBlockifiedTemplate( attributes ) );
|
|
|
|
|
|
|
|
const blocks = getBlocks();
|
|
|
|
|
|
|
|
const groupBlock = blocks.find(
|
|
|
|
( block ) =>
|
|
|
|
block.name === 'core/group' &&
|
|
|
|
block.innerBlocks.some(
|
|
|
|
( innerBlock ) =>
|
|
|
|
innerBlock.name === 'woocommerce/store-notices'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( groupBlock ) {
|
|
|
|
selectBlock( groupBlock.clientId );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Title shown within the block itself.
|
|
|
|
*/
|
|
|
|
const getTitle = () => {
|
2023-12-12 22:12:36 +00:00
|
|
|
return __( 'Classic Cart', 'woocommerce' );
|
2023-09-19 09:58:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description shown within the block itself.
|
|
|
|
*/
|
|
|
|
const getDescription = () => {
|
|
|
|
return __(
|
|
|
|
'This block will render the classic cart shortcode. You can optionally transform it into blocks for more control over the cart experience.',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-09-19 09:58:18 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const blockifyConfig = {
|
|
|
|
getButtonLabel,
|
|
|
|
onClickCallback,
|
|
|
|
getBlockifiedTemplate,
|
|
|
|
};
|
|
|
|
|
|
|
|
export { blockifyConfig, isConversionPossible, getDescription, getTitle };
|