2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-09-16 12:16:21 +00:00
|
|
|
import classnames from 'classnames';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { fields } from '@woocommerce/icons';
|
|
|
|
import { Icon } from '@wordpress/icons';
|
2022-08-15 05:45:15 +00:00
|
|
|
import { registerBlockType, createBlock } from '@wordpress/blocks';
|
2022-04-08 13:47:19 +00:00
|
|
|
import type { BlockInstance } from '@wordpress/blocks';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Edit, Save } from './edit';
|
2022-01-28 16:37:06 +00:00
|
|
|
import { blockAttributes, deprecatedAttributes } from './attributes';
|
2021-07-22 11:03:00 +00:00
|
|
|
import './inner-blocks';
|
2022-01-28 16:37:06 +00:00
|
|
|
import metadata from './block.json';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
const settings = {
|
|
|
|
icon: {
|
2021-12-10 14:32:44 +00:00
|
|
|
src: (
|
|
|
|
<Icon
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ fields }
|
2021-12-10 14:32:44 +00:00
|
|
|
className="wc-block-editor-components-block-icon"
|
|
|
|
/>
|
|
|
|
),
|
2021-07-22 11:03:00 +00:00
|
|
|
},
|
2022-01-28 16:37:06 +00:00
|
|
|
attributes: {
|
|
|
|
...metadata.attributes,
|
|
|
|
...blockAttributes,
|
|
|
|
...deprecatedAttributes,
|
2021-07-22 11:03:00 +00:00
|
|
|
},
|
|
|
|
edit: Edit,
|
|
|
|
save: Save,
|
2023-10-19 15:43:43 +00:00
|
|
|
transforms: {
|
|
|
|
to: [
|
|
|
|
{
|
|
|
|
type: 'block',
|
|
|
|
blocks: [ 'woocommerce/classic-shortcode' ],
|
|
|
|
transform: ( attributes ) => {
|
|
|
|
return createBlock(
|
|
|
|
'woocommerce/classic-shortcode',
|
|
|
|
{
|
|
|
|
shortcode: 'checkout',
|
|
|
|
align: attributes.align,
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-09-16 12:16:21 +00:00
|
|
|
// Migrates v1 to v2 checkout.
|
|
|
|
deprecated: [
|
|
|
|
{
|
2022-01-28 16:37:06 +00:00
|
|
|
attributes: {
|
|
|
|
...metadata.attributes,
|
|
|
|
...blockAttributes,
|
|
|
|
...deprecatedAttributes,
|
|
|
|
},
|
2021-09-16 12:16:21 +00:00
|
|
|
save( { attributes }: { attributes: { className: string } } ) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'is-loading',
|
|
|
|
attributes.className
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
2021-07-22 11:03:00 +00:00
|
|
|
},
|
2021-11-19 10:49:33 +00:00
|
|
|
migrate: ( attributes: {
|
|
|
|
showOrderNotes: boolean;
|
|
|
|
showPolicyLinks: boolean;
|
|
|
|
showReturnToCart: boolean;
|
|
|
|
cartPageId: number;
|
|
|
|
} ) => {
|
|
|
|
const {
|
|
|
|
showOrderNotes,
|
|
|
|
showPolicyLinks,
|
|
|
|
showReturnToCart,
|
|
|
|
cartPageId,
|
|
|
|
} = attributes;
|
|
|
|
return [
|
|
|
|
attributes,
|
|
|
|
[
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-fields-block',
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-express-payment-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-contact-information-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-shipping-address-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-billing-address-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-shipping-methods-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-payment-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
2024-01-10 14:20:06 +00:00
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-additional-information-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
),
|
2021-11-19 10:49:33 +00:00
|
|
|
showOrderNotes
|
|
|
|
? createBlock(
|
|
|
|
'woocommerce/checkout-order-note-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
: false,
|
|
|
|
showPolicyLinks
|
|
|
|
? createBlock(
|
|
|
|
'woocommerce/checkout-terms-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
: false,
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-actions-block',
|
|
|
|
{
|
|
|
|
showReturnToCart,
|
|
|
|
cartPageId,
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
].filter( Boolean ) as BlockInstance[]
|
|
|
|
),
|
|
|
|
createBlock( 'woocommerce/checkout-totals-block', {} ),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
},
|
|
|
|
isEligible: (
|
|
|
|
attributes: Record< string, unknown >,
|
|
|
|
innerBlocks: BlockInstance[]
|
|
|
|
) => {
|
|
|
|
return ! innerBlocks.some(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name === 'woocommerce/checkout-fields-block'
|
|
|
|
);
|
|
|
|
},
|
2021-09-16 12:16:21 +00:00
|
|
|
},
|
2024-01-10 14:20:06 +00:00
|
|
|
// Adds the additional information block.
|
|
|
|
{
|
|
|
|
save( { attributes }: { attributes: { className: string } } ) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames(
|
|
|
|
'is-loading',
|
|
|
|
attributes.className
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
isEligible: (
|
|
|
|
_attributes: Record< string, unknown >,
|
|
|
|
innerBlocks: BlockInstance[]
|
|
|
|
) => {
|
|
|
|
const checkoutFieldsBlock = innerBlocks.find(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name === 'woocommerce/checkout-fields-block'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! checkoutFieldsBlock ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top level block is the fields block, we then need to search within that for the additional information block.
|
|
|
|
return ! checkoutFieldsBlock.innerBlocks.some(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name ===
|
|
|
|
'woocommerce/checkout-additional-information-block'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
migrate: (
|
|
|
|
attributes: Record< string, unknown >,
|
|
|
|
innerBlocks: BlockInstance[]
|
|
|
|
) => {
|
|
|
|
const checkoutFieldsBlockIndex = innerBlocks.findIndex(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name === 'woocommerce/checkout-fields-block'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( checkoutFieldsBlockIndex === -1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkoutFieldsBlock =
|
|
|
|
innerBlocks[ checkoutFieldsBlockIndex ];
|
|
|
|
|
|
|
|
const insertIndex = checkoutFieldsBlock.innerBlocks.findIndex(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name ===
|
|
|
|
'wp-block-woocommerce-checkout-payment-block'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( insertIndex === -1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
innerBlocks[ checkoutFieldsBlockIndex ] =
|
|
|
|
checkoutFieldsBlock.innerBlocks
|
|
|
|
.slice( 0, insertIndex )
|
|
|
|
.concat(
|
|
|
|
createBlock(
|
|
|
|
'woocommerce/checkout-additional-information-block',
|
|
|
|
{},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.concat(
|
|
|
|
innerBlocks.slice(
|
|
|
|
insertIndex + 1,
|
|
|
|
innerBlocks.length
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return [ attributes, innerBlocks ];
|
|
|
|
},
|
|
|
|
},
|
2021-09-16 12:16:21 +00:00
|
|
|
],
|
2021-07-22 11:03:00 +00:00
|
|
|
};
|
|
|
|
|
2022-08-15 05:45:15 +00:00
|
|
|
registerBlockType( metadata, settings );
|