2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-03-17 12:26:34 +00:00
|
|
|
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
|
2021-07-22 11:03:00 +00:00
|
|
|
import PageSelector from '@woocommerce/editor-components/page-selector';
|
2021-11-26 14:47:37 +00:00
|
|
|
import { PanelBody, ToggleControl } from '@wordpress/components';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { CHECKOUT_PAGE_ID } from '@woocommerce/block-settings';
|
2022-12-15 10:28:23 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import { ReturnToCartButton } from '@woocommerce/base-components/cart-checkout';
|
2023-03-17 12:26:34 +00:00
|
|
|
import EditableButton from '@woocommerce/editor-components/editable-button';
|
2021-11-26 14:47:37 +00:00
|
|
|
import Noninteractive from '@woocommerce/base-components/noninteractive';
|
2022-12-15 10:28:23 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-12-15 10:28:23 +00:00
|
|
|
import { defaultPlaceOrderButtonLabel } from './constants';
|
2021-09-24 13:44:05 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
export const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: {
|
|
|
|
showReturnToCart: boolean;
|
|
|
|
cartPageId: number;
|
2022-12-15 10:28:23 +00:00
|
|
|
placeOrderButtonLabel: string;
|
2021-07-22 11:03:00 +00:00
|
|
|
};
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
|
|
|
} ): JSX.Element => {
|
2021-09-24 13:44:05 +00:00
|
|
|
const blockProps = useBlockProps();
|
2022-12-15 10:28:23 +00:00
|
|
|
const {
|
|
|
|
cartPageId = 0,
|
|
|
|
showReturnToCart = true,
|
|
|
|
placeOrderButtonLabel,
|
|
|
|
} = attributes;
|
2021-07-22 11:03:00 +00:00
|
|
|
const { current: savedCartPageId } = useRef( cartPageId );
|
|
|
|
const currentPostId = useSelect(
|
|
|
|
( select ) => {
|
|
|
|
if ( ! savedCartPageId ) {
|
|
|
|
const store = select( 'core/editor' );
|
|
|
|
return store.getCurrentPostId();
|
|
|
|
}
|
|
|
|
return savedCartPageId;
|
|
|
|
},
|
|
|
|
[ savedCartPageId ]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
|
|
|
<InspectorControls>
|
2023-12-12 23:05:20 +00:00
|
|
|
<PanelBody title={ __( 'Account options', 'woocommerce' ) }>
|
2021-07-22 11:03:00 +00:00
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show a "Return to Cart" link',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2021-07-22 11:03:00 +00:00
|
|
|
) }
|
|
|
|
checked={ showReturnToCart }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showReturnToCart: ! showReturnToCart,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
{ showReturnToCart &&
|
|
|
|
! (
|
|
|
|
currentPostId === CHECKOUT_PAGE_ID &&
|
|
|
|
savedCartPageId === 0
|
|
|
|
) && (
|
|
|
|
<PageSelector
|
|
|
|
pageId={ cartPageId }
|
|
|
|
setPageId={ ( id: number ) =>
|
|
|
|
setAttributes( { cartPageId: id } )
|
|
|
|
}
|
|
|
|
labels={ {
|
|
|
|
title: __(
|
|
|
|
'Return to Cart button',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2021-07-22 11:03:00 +00:00
|
|
|
),
|
|
|
|
default: __(
|
|
|
|
'WooCommerce Cart Page',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2021-07-22 11:03:00 +00:00
|
|
|
),
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</InspectorControls>
|
2022-12-15 10:28:23 +00:00
|
|
|
<div className="wc-block-checkout__actions">
|
2023-04-19 13:09:06 +00:00
|
|
|
<div className="wc-block-checkout__actions_row">
|
|
|
|
<Noninteractive>
|
|
|
|
{ showReturnToCart && (
|
|
|
|
<ReturnToCartButton
|
|
|
|
link={ getSetting(
|
|
|
|
'page-' + cartPageId,
|
|
|
|
false
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</Noninteractive>
|
|
|
|
<EditableButton
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx(
|
2023-06-05 14:31:32 +00:00
|
|
|
'wc-block-cart__submit-button',
|
|
|
|
'wc-block-components-checkout-place-order-button',
|
|
|
|
{
|
|
|
|
'wc-block-components-checkout-place-order-button--full-width':
|
|
|
|
! showReturnToCart,
|
|
|
|
}
|
|
|
|
) }
|
2023-04-19 13:09:06 +00:00
|
|
|
value={ placeOrderButtonLabel }
|
|
|
|
placeholder={ defaultPlaceOrderButtonLabel }
|
|
|
|
onChange={ ( content ) => {
|
|
|
|
setAttributes( {
|
|
|
|
placeOrderButtonLabel: content,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</div>
|
2022-12-15 10:28:23 +00:00
|
|
|
</div>
|
2021-07-22 11:03:00 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return <div { ...useBlockProps.save() } />;
|
|
|
|
};
|