2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-16 14:59:16 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-08-09 20:18:36 +00:00
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/block-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';
|
2020-01-16 14:50:48 +00:00
|
|
|
import ViewSwitcher from '@woocommerce/block-components/view-switcher';
|
2020-04-22 16:44:12 +00:00
|
|
|
import PageSelector from '@woocommerce/block-components/page-selector';
|
2020-03-17 10:30:52 +00:00
|
|
|
import { SHIPPING_ENABLED, 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-03-17 10:30:52 +00:00
|
|
|
import { __experimentalCreateInterpolateElement } from 'wordpress-element';
|
2020-04-22 16:44:12 +00:00
|
|
|
import { useRef } from '@wordpress/element';
|
2020-03-17 10:30:52 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2020-04-08 15:03:39 +00:00
|
|
|
import { previewCart, cartBlockPreview } from '@woocommerce/resource-previews';
|
|
|
|
|
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,
|
|
|
|
isShippingCostHidden,
|
|
|
|
checkoutPageId,
|
|
|
|
} = 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"
|
|
|
|
>
|
|
|
|
{ __experimentalCreateInterpolateElement(
|
|
|
|
__(
|
|
|
|
'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>
|
|
|
|
) }
|
2020-04-22 16:44:12 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
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,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Hide shipping costs until an address is entered',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'If checked, shipping rates will be hidden until the customer uses the shipping calculator or enters their address during checkout.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ isShippingCostHidden }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
isShippingCostHidden: ! isShippingCostHidden,
|
2020-03-17 10:30:52 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</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-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-03-16 16:38:24 +00:00
|
|
|
*/
|
|
|
|
const CartEditor = ( { className, attributes, setAttributes } ) => {
|
2020-04-08 15:03:39 +00:00
|
|
|
if ( attributes.isPreview ) {
|
|
|
|
return cartBlockPreview;
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:08:48 +00:00
|
|
|
return (
|
2019-12-10 15:41:57 +00:00
|
|
|
<div className={ className }>
|
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
|
|
|
) }
|
|
|
|
/>
|
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;
|