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';
|
2024-10-31 16:49:23 +00:00
|
|
|
import { PanelBody, ToggleControl, TextControl } 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';
|
2024-10-31 16:49:23 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context';
|
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
|
|
|
import { FormattedMonetaryAmount } from '@woocommerce/blocks-components';
|
2022-12-15 10:28:23 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-10-31 16:49:23 +00:00
|
|
|
import { BlockAttributes } from './block';
|
|
|
|
import './editor.scss';
|
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,
|
|
|
|
}: {
|
2024-10-31 16:49:23 +00:00
|
|
|
attributes: BlockAttributes;
|
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;
|
2024-10-31 16:49:23 +00:00
|
|
|
const { cartTotals } = useStoreCart();
|
|
|
|
const totalsCurrency = getCurrencyFromPriceResponse( cartTotals );
|
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 ]
|
|
|
|
);
|
|
|
|
|
2024-10-31 16:49:23 +00:00
|
|
|
const showPrice = blockProps.className.includes( 'is-style-with-price' );
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
|
|
|
<InspectorControls>
|
2024-10-31 16:49:23 +00:00
|
|
|
<PanelBody title={ __( '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,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2024-10-31 16:49:23 +00:00
|
|
|
|
|
|
|
{ showPrice && (
|
|
|
|
<TextControl
|
|
|
|
label={ __( 'Price separator', 'woocommerce' ) }
|
|
|
|
id="price-separator"
|
|
|
|
value={ attributes.priceSeparator }
|
|
|
|
onChange={ ( value ) => {
|
|
|
|
setAttributes( {
|
|
|
|
priceSeparator: value,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
2021-07-22 11:03:00 +00:00
|
|
|
</PanelBody>
|
2024-10-31 16:49:23 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
{ 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-10-31 16:49:23 +00:00
|
|
|
>
|
|
|
|
{ showPrice && (
|
|
|
|
<>
|
|
|
|
<style>
|
|
|
|
{ `.wp-block-woocommerce-checkout-actions-block {
|
|
|
|
.wc-block-components-checkout-place-order-button__separator {
|
|
|
|
&::after {
|
|
|
|
content: "${ attributes.priceSeparator }";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}` }
|
|
|
|
</style>
|
|
|
|
<div className="wc-block-components-checkout-place-order-button__separator"></div>
|
|
|
|
<div className="wc-block-components-checkout-place-order-button__price">
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
value={ cartTotals.total_price }
|
|
|
|
currency={ totalsCurrency }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) }
|
|
|
|
</EditableButton>
|
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() } />;
|
|
|
|
};
|