2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-08-13 13:24:51 +00:00
|
|
|
import classnames from 'classnames';
|
2019-12-16 14:59:16 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-09-02 08:21:46 +00:00
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
2020-02-19 16:33:10 +00:00
|
|
|
import { InspectorControls } from '@wordpress/block-editor';
|
2020-03-17 10:30:52 +00:00
|
|
|
import {
|
|
|
|
Disabled,
|
|
|
|
PanelBody,
|
|
|
|
ToggleControl,
|
|
|
|
Notice,
|
|
|
|
} from '@wordpress/components';
|
2019-12-10 15:41:57 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2021-03-19 10:05:42 +00:00
|
|
|
import { CartCheckoutCompatibilityNotice } from '@woocommerce/editor-components/compatibility-notices';
|
2020-09-02 08:21:46 +00:00
|
|
|
import ViewSwitcher from '@woocommerce/editor-components/view-switcher';
|
|
|
|
import PageSelector from '@woocommerce/editor-components/page-selector';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { CART_PAGE_ID } from '@woocommerce/block-settings';
|
2020-03-06 11:43:40 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2020-04-01 09:27:53 +00:00
|
|
|
import {
|
|
|
|
EditorProvider,
|
|
|
|
useEditorContext,
|
|
|
|
CartProvider,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-09-26 13:51:21 +00:00
|
|
|
import { createInterpolateElement } from 'wordpress-element';
|
2020-04-22 16:44:12 +00:00
|
|
|
import { useRef } from '@wordpress/element';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getAdminLink, getSetting } from '@woocommerce/settings';
|
2020-08-13 13:24:51 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2020-04-08 15:03:39 +00:00
|
|
|
|
2019-12-05 21:08:48 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-04-08 15:03:39 +00:00
|
|
|
import Block from './block.js';
|
|
|
|
import EmptyCartEdit from './empty-cart-edit';
|
2020-03-17 10:30:52 +00:00
|
|
|
import './editor.scss';
|
2019-12-03 13:57:56 +00:00
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
const BlockSettings = ( { attributes, setAttributes } ) => {
|
2020-03-17 10:30:52 +00:00
|
|
|
const {
|
|
|
|
isShippingCalculatorEnabled,
|
|
|
|
checkoutPageId,
|
2020-08-14 11:08:16 +00:00
|
|
|
hasDarkControls,
|
2021-05-17 14:00:57 +00:00
|
|
|
showRateAfterTaxName,
|
2020-03-17 10:30:52 +00:00
|
|
|
} = attributes;
|
|
|
|
const { currentPostId } = useEditorContext();
|
2020-04-22 16:44:12 +00:00
|
|
|
const { current: savedCheckoutPageId } = useRef( checkoutPageId );
|
2020-03-16 16:38:24 +00:00
|
|
|
return (
|
2020-02-19 16:33:10 +00:00
|
|
|
<InspectorControls>
|
2020-03-17 10:30:52 +00:00
|
|
|
{ currentPostId !== CART_PAGE_ID && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-cart__page-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
status="warning"
|
|
|
|
>
|
2020-09-26 13:51:21 +00:00
|
|
|
{ createInterpolateElement(
|
2020-03-17 10:30:52 +00:00
|
|
|
__(
|
|
|
|
'If you would like to use this block as your default cart you must update your <a>page settings in WooCommerce</a>.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
a: (
|
|
|
|
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
|
|
|
<a
|
|
|
|
href={ getAdminLink(
|
|
|
|
'admin.php?page=wc-settings&tab=advanced'
|
|
|
|
) }
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
</Notice>
|
|
|
|
) }
|
2021-04-22 11:37:27 +00:00
|
|
|
{ getSetting( 'shippingEnabled', true ) && (
|
2020-03-17 10:30:52 +00:00
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
2020-04-22 16:44:12 +00:00
|
|
|
'Shipping rates',
|
2020-03-17 10:30:52 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-04-22 16:44:12 +00:00
|
|
|
<ToggleControl
|
2020-03-17 10:30:52 +00:00
|
|
|
label={ __(
|
2020-04-22 16:44:12 +00:00
|
|
|
'Shipping calculator',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Allow customers to estimate shipping by entering their address.',
|
2020-03-17 10:30:52 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-04-22 16:44:12 +00:00
|
|
|
checked={ isShippingCalculatorEnabled }
|
|
|
|
onChange={ () =>
|
2020-03-17 10:30:52 +00:00
|
|
|
setAttributes( {
|
2020-04-22 16:44:12 +00:00
|
|
|
isShippingCalculatorEnabled: ! isShippingCalculatorEnabled,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2020-03-17 10:30:52 +00:00
|
|
|
</PanelBody>
|
|
|
|
) }
|
2021-05-17 14:00:57 +00:00
|
|
|
{ getSetting( 'taxesEnabled' ) &&
|
|
|
|
getSetting( 'displayItemizedTaxes', false ) &&
|
|
|
|
! getSetting( 'displayCartPricesIncludingTax', false ) && (
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Taxes', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show rate after tax name',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Show the percentage rate alongside each tax line in the summary.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showRateAfterTaxName }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showRateAfterTaxName: ! showRateAfterTaxName,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
) }
|
2020-04-22 16:44:12 +00:00
|
|
|
{ ! (
|
|
|
|
currentPostId === CART_PAGE_ID && savedCheckoutPageId === 0
|
|
|
|
) && (
|
|
|
|
<PageSelector
|
|
|
|
pageId={ checkoutPageId }
|
|
|
|
setPageId={ ( id ) =>
|
|
|
|
setAttributes( { checkoutPageId: id } )
|
|
|
|
}
|
|
|
|
labels={ {
|
|
|
|
title: __(
|
|
|
|
'Proceed to Checkout button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
default: __(
|
|
|
|
'WooCommerce Checkout Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
2020-08-14 11:08:16 +00:00
|
|
|
<PanelBody title={ __( 'Style', 'woo-gutenberg-products-block' ) }>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Dark mode inputs',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Inputs styled specifically for use on dark background colors.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ hasDarkControls }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
hasDarkControls: ! hasDarkControls,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2020-08-09 20:18:36 +00:00
|
|
|
<CartCheckoutFeedbackPrompt />
|
2020-02-19 16:33:10 +00:00
|
|
|
</InspectorControls>
|
|
|
|
);
|
2020-03-16 16:38:24 +00:00
|
|
|
};
|
2020-02-19 16:33:10 +00:00
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "Cart Block".
|
2020-05-01 09:33:55 +00:00
|
|
|
*
|
|
|
|
* Note: We need to always render `<InnerBlocks>` in the editor. Otherwise,
|
|
|
|
* if the user saves the page without having triggered the 'Empty Cart'
|
|
|
|
* view, inner blocks would not be saved and they wouldn't be visible
|
|
|
|
* in the frontend.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {string} props.className CSS class used.
|
|
|
|
* @param {Object} props.attributes Attributes available.
|
|
|
|
* @param {function(any):any} props.setAttributes Setter for attributes.
|
2020-03-16 16:38:24 +00:00
|
|
|
*/
|
|
|
|
const CartEditor = ( { className, attributes, setAttributes } ) => {
|
2019-12-05 21:08:48 +00:00
|
|
|
return (
|
2020-08-13 13:24:51 +00:00
|
|
|
<div
|
|
|
|
className={ classnames( className, 'wp-block-woocommerce-cart', {
|
|
|
|
'is-editor-preview': attributes.isPreview,
|
|
|
|
} ) }
|
|
|
|
>
|
2020-01-16 14:50:48 +00:00
|
|
|
<ViewSwitcher
|
|
|
|
label={ __( 'Edit', 'woo-gutenberg-products-block' ) }
|
|
|
|
views={ [
|
|
|
|
{
|
|
|
|
value: 'full',
|
|
|
|
name: __( 'Full Cart', 'woo-gutenberg-products-block' ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'empty',
|
|
|
|
name: __(
|
|
|
|
'Empty Cart',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
defaultView={ 'full' }
|
|
|
|
render={ ( currentView ) => (
|
2020-04-08 15:03:39 +00:00
|
|
|
<BlockErrorBoundary
|
|
|
|
header={ __(
|
|
|
|
'Cart Block Error',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
text={ __(
|
|
|
|
'There was an error whilst rendering the cart block. If this problem continues, try re-creating the block.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
showErrorMessage={ true }
|
|
|
|
errorMessagePrefix={ __(
|
|
|
|
'Error message:',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-01-16 14:50:48 +00:00
|
|
|
{ currentView === 'full' && (
|
2020-02-19 16:33:10 +00:00
|
|
|
<>
|
2020-04-22 16:44:12 +00:00
|
|
|
<EditorProvider previewData={ { previewCart } }>
|
2020-03-16 16:38:24 +00:00
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
2020-04-22 16:44:12 +00:00
|
|
|
<Disabled>
|
2020-04-08 15:03:39 +00:00
|
|
|
<CartProvider>
|
|
|
|
<Block attributes={ attributes } />
|
|
|
|
</CartProvider>
|
2020-04-22 16:44:12 +00:00
|
|
|
</Disabled>
|
|
|
|
</EditorProvider>
|
2020-05-01 09:33:55 +00:00
|
|
|
<EmptyCartEdit hidden={ true } />
|
2020-02-19 16:33:10 +00:00
|
|
|
</>
|
2020-01-16 14:50:48 +00:00
|
|
|
) }
|
2020-05-01 09:33:55 +00:00
|
|
|
{ currentView === 'empty' && <EmptyCartEdit /> }
|
2020-04-08 15:03:39 +00:00
|
|
|
</BlockErrorBoundary>
|
2020-01-16 14:50:48 +00:00
|
|
|
) }
|
|
|
|
/>
|
2021-03-19 10:05:42 +00:00
|
|
|
<CartCheckoutCompatibilityNotice blockName="cart" />
|
2019-12-10 15:41:57 +00:00
|
|
|
</div>
|
2019-12-05 21:08:48 +00:00
|
|
|
);
|
2019-12-03 13:57:56 +00:00
|
|
|
};
|
|
|
|
|
2019-12-10 15:41:57 +00:00
|
|
|
CartEditor.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
};
|
2019-12-03 13:57:56 +00:00
|
|
|
|
2020-03-17 10:30:52 +00:00
|
|
|
export default CartEditor;
|