2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-16 14:59:16 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-17 10:30:52 +00:00
|
|
|
import FeedbackPrompt 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,
|
|
|
|
SelectControl,
|
|
|
|
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-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 { useSelect } from '@wordpress/data';
|
|
|
|
import { __experimentalCreateInterpolateElement } from 'wordpress-element';
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2020-03-25 21:12:34 +00:00
|
|
|
import { recordEditorEvent } from '@woocommerce/base-utils';
|
2019-12-05 21:08:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-12-10 15:41:57 +00:00
|
|
|
import FullCart from './full-cart';
|
|
|
|
import EmptyCart from './empty-cart';
|
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 pages =
|
|
|
|
useSelect( ( select ) => {
|
|
|
|
return select( 'core' ).getEntityRecords( 'postType', 'page', {
|
|
|
|
status: 'publish',
|
|
|
|
orderby: 'title',
|
|
|
|
order: 'asc',
|
|
|
|
per_page: 100,
|
|
|
|
} );
|
|
|
|
}, [] ) || null;
|
|
|
|
const { currentPostId } = useEditorContext();
|
2020-02-19 16:33:10 +00:00
|
|
|
|
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-02-19 16:33:10 +00:00
|
|
|
<PanelBody
|
2020-03-13 13:41:59 +00:00
|
|
|
title={ __( 'Shipping rates', 'woo-gutenberg-products-block' ) }
|
2020-02-19 16:33:10 +00:00
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Shipping calculator',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
2020-03-13 13:41:59 +00:00
|
|
|
'Allow customers to estimate shipping by entering their address.',
|
2020-02-19 16:33:10 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ isShippingCalculatorEnabled }
|
2020-03-25 21:12:34 +00:00
|
|
|
onChange={ () => {
|
|
|
|
recordEditorEvent(
|
|
|
|
'cart_settings_shipping_calculator_toggle',
|
|
|
|
{
|
|
|
|
enabled: ! isShippingCalculatorEnabled,
|
|
|
|
}
|
|
|
|
);
|
2020-02-19 16:33:10 +00:00
|
|
|
setAttributes( {
|
|
|
|
isShippingCalculatorEnabled: ! isShippingCalculatorEnabled,
|
2020-03-25 21:12:34 +00:00
|
|
|
} );
|
|
|
|
} }
|
2020-02-19 16:33:10 +00:00
|
|
|
/>
|
2020-03-13 13:41:59 +00:00
|
|
|
<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-02-19 16:33:10 +00:00
|
|
|
</PanelBody>
|
2020-03-17 10:30:52 +00:00
|
|
|
{ ( currentPostId !== CART_PAGE_ID || checkoutPageId ) && pages && (
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Proceed to Checkout button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<SelectControl
|
|
|
|
label={ __(
|
|
|
|
'Link to',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ checkoutPageId }
|
|
|
|
options={ [
|
|
|
|
...[
|
|
|
|
{
|
|
|
|
label: __(
|
|
|
|
'WooCommerce Checkout Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
...Object.values( pages ).map( ( page ) => {
|
|
|
|
return {
|
|
|
|
label: page.title.raw,
|
|
|
|
value: parseInt( page.id, 10 ),
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
] }
|
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( {
|
|
|
|
checkoutPageId: parseInt( value, 10 ),
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
) }
|
|
|
|
<FeedbackPrompt
|
|
|
|
text={ __(
|
|
|
|
'We are currently working on improving our cart and checkout blocks, providing merchants with the tools and customization options they need.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
/>
|
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".
|
|
|
|
*/
|
|
|
|
const CartEditor = ( { className, attributes, setAttributes } ) => {
|
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-02-19 16:33:10 +00:00
|
|
|
<>
|
2020-01-16 14:50:48 +00:00
|
|
|
{ currentView === 'full' && (
|
2020-02-19 16:33:10 +00:00
|
|
|
<>
|
2020-03-16 16:38:24 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
|
|
|
) }
|
2020-03-06 11:43:40 +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'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<Disabled>
|
2020-03-13 13:41:59 +00:00
|
|
|
<EditorProvider>
|
2020-04-01 09:27:53 +00:00
|
|
|
<CartProvider>
|
|
|
|
<FullCart
|
|
|
|
attributes={ attributes }
|
|
|
|
/>
|
|
|
|
</CartProvider>
|
2020-03-13 13:41:59 +00:00
|
|
|
</EditorProvider>
|
2020-03-06 11:43:40 +00:00
|
|
|
</Disabled>
|
|
|
|
</BlockErrorBoundary>
|
2020-02-19 16:33:10 +00:00
|
|
|
</>
|
2020-01-16 14:50:48 +00:00
|
|
|
) }
|
2020-03-06 11:43:40 +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'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<EmptyCart hidden={ currentView === 'full' } />
|
|
|
|
</BlockErrorBoundary>
|
2020-02-19 16:33:10 +00:00
|
|
|
</>
|
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;
|