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';
|
2024-10-28 11:03:14 +00:00
|
|
|
import {
|
|
|
|
InspectorControls,
|
|
|
|
RichText,
|
|
|
|
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 { ReturnToCartButton } from '@woocommerce/base-components/cart-checkout';
|
2023-03-17 12:26:34 +00:00
|
|
|
import EditableButton from '@woocommerce/editor-components/editable-button';
|
2022-12-15 10:28:23 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-10-28 11:03:14 +00:00
|
|
|
import {
|
|
|
|
defaultPlaceOrderButtonLabel,
|
|
|
|
defaultReturnToCartButtonLabel,
|
|
|
|
} 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;
|
2024-10-28 11:03:14 +00:00
|
|
|
returnToCartButtonLabel: 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,
|
2024-06-25 15:47:31 +00:00
|
|
|
showReturnToCart = false,
|
2022-12-15 10:28:23 +00:00
|
|
|
placeOrderButtonLabel,
|
2024-10-28 11:03:14 +00:00
|
|
|
returnToCartButtonLabel,
|
2022-12-15 10:28:23 +00:00
|
|
|
} = 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>
|
2024-06-25 15:47:31 +00:00
|
|
|
<PanelBody title={ __( 'Navigation 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
|
|
|
) }
|
2024-06-25 15:47:31 +00:00
|
|
|
help={ __(
|
|
|
|
'Recommended to enable only if there is no Cart link in the header.',
|
|
|
|
'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">
|
2024-10-28 11:03:14 +00:00
|
|
|
{ showReturnToCart && (
|
|
|
|
<ReturnToCartButton href="#cart-page-placeholder">
|
|
|
|
<RichText
|
|
|
|
multiline={ false }
|
|
|
|
allowedFormats={ [] }
|
|
|
|
value={ returnToCartButtonLabel }
|
|
|
|
placeholder={ defaultReturnToCartButtonLabel }
|
|
|
|
onChange={ ( content ) => {
|
|
|
|
setAttributes( {
|
|
|
|
returnToCartButtonLabel: content,
|
|
|
|
} );
|
|
|
|
} }
|
2023-04-19 13:09:06 +00:00
|
|
|
/>
|
2024-10-28 11:03:14 +00:00
|
|
|
</ReturnToCartButton>
|
|
|
|
) }
|
2023-04-19 13:09:06 +00:00
|
|
|
<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,
|
|
|
|
} );
|
|
|
|
} }
|
2024-07-24 16:08:05 +00:00
|
|
|
/>
|
2023-04-19 13:09:06 +00:00
|
|
|
</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() } />;
|
|
|
|
};
|