2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-16 14:59:16 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-16 16:38:24 +00:00
|
|
|
import FeedbackPrompt from '@woocommerce/block-components/feedback-prompt';
|
2020-03-04 15:13:38 +00:00
|
|
|
import { InspectorControls } from '@wordpress/block-editor';
|
2020-03-05 13:06:47 +00:00
|
|
|
import {
|
|
|
|
PanelBody,
|
|
|
|
ToggleControl,
|
|
|
|
CheckboxControl,
|
2020-03-16 16:38:24 +00:00
|
|
|
SelectControl,
|
2020-03-06 12:20:17 +00:00
|
|
|
Notice,
|
2020-04-06 15:35:09 +00:00
|
|
|
Disabled,
|
2020-03-05 13:06:47 +00:00
|
|
|
} from '@wordpress/components';
|
2020-03-06 11:43:40 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2020-03-06 12:20:17 +00:00
|
|
|
import {
|
|
|
|
PRIVACY_URL,
|
|
|
|
TERMS_URL,
|
2020-03-16 16:38:24 +00:00
|
|
|
CHECKOUT_PAGE_ID,
|
2020-03-06 12:20:17 +00:00
|
|
|
} from '@woocommerce/block-settings';
|
2020-03-16 16:38:24 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-03-06 12:20:17 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
|
|
|
import { __experimentalCreateInterpolateElement } from 'wordpress-element';
|
2020-03-16 16:38:24 +00:00
|
|
|
import { EditorProvider, useEditorContext } from '@woocommerce/base-context';
|
2020-04-07 15:41:22 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2019-12-03 14:12:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from './block.js';
|
2019-12-06 13:18:55 +00:00
|
|
|
import './editor.scss';
|
2019-12-03 14:12:46 +00:00
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
const BlockSettings = ( { attributes, setAttributes } ) => {
|
2020-03-05 13:06:47 +00:00
|
|
|
const {
|
|
|
|
showCompanyField,
|
|
|
|
showAddress2Field,
|
|
|
|
showPhoneField,
|
|
|
|
requireCompanyField,
|
|
|
|
requirePhoneField,
|
2020-03-06 12:20:17 +00:00
|
|
|
showPolicyLinks,
|
2020-03-16 16:38:24 +00:00
|
|
|
showReturnToCart,
|
|
|
|
cartPageId,
|
2020-03-05 13:06:47 +00:00
|
|
|
} = attributes;
|
2020-03-16 16:38:24 +00:00
|
|
|
const { currentPostId } = useEditorContext();
|
|
|
|
const pages =
|
|
|
|
useSelect( ( select ) => {
|
|
|
|
return select( 'core' ).getEntityRecords( 'postType', 'page', {
|
|
|
|
status: 'publish',
|
|
|
|
orderby: 'title',
|
|
|
|
order: 'asc',
|
|
|
|
per_page: 100,
|
|
|
|
} );
|
|
|
|
}, [] ) || null;
|
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-03-16 16:38:24 +00:00
|
|
|
<InspectorControls>
|
|
|
|
{ currentPostId !== CHECKOUT_PAGE_ID && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-checkout__page-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
status="warning"
|
2020-03-05 13:06:47 +00:00
|
|
|
>
|
2020-03-16 16:38:24 +00:00
|
|
|
{ __experimentalCreateInterpolateElement(
|
|
|
|
__(
|
|
|
|
'If you would like to use this block as your default checkout you must update your <a>page settings in WooCommerce</a>.',
|
2020-03-05 13:06:47 +00:00
|
|
|
'woo-gutenberg-products-block'
|
2020-03-16 16:38:24 +00:00
|
|
|
),
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
) }
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Form options', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
<p className="wc-block-checkout__controls-text">
|
|
|
|
{ __(
|
|
|
|
'Choose whether your checkout form requires extra information from customers.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __( 'Company', 'woo-gutenberg-products-block' ) }
|
|
|
|
checked={ showCompanyField }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showCompanyField: ! showCompanyField,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{ showCompanyField && (
|
|
|
|
<CheckboxControl
|
2020-03-05 13:06:47 +00:00
|
|
|
label={ __(
|
2020-03-16 16:38:24 +00:00
|
|
|
'Require company name?',
|
2020-03-05 13:06:47 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
checked={ requireCompanyField }
|
2020-03-05 13:06:47 +00:00
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
2020-03-16 16:38:24 +00:00
|
|
|
requireCompanyField: ! requireCompanyField,
|
2020-03-05 13:06:47 +00:00
|
|
|
} )
|
|
|
|
}
|
2020-03-16 16:38:24 +00:00
|
|
|
className="components-base-control--nested"
|
2020-03-05 13:06:47 +00:00
|
|
|
/>
|
2020-03-16 16:38:24 +00:00
|
|
|
) }
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Apartment, suite, etc.',
|
|
|
|
'woo-gutenberg-products-block'
|
2020-03-05 13:06:47 +00:00
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
checked={ showAddress2Field }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showAddress2Field: ! showAddress2Field,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __( 'Phone', 'woo-gutenberg-products-block' ) }
|
|
|
|
checked={ showPhoneField }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showPhoneField: ! showPhoneField,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{ showPhoneField && (
|
|
|
|
<CheckboxControl
|
2020-03-05 13:06:47 +00:00
|
|
|
label={ __(
|
2020-03-16 16:38:24 +00:00
|
|
|
'Require phone number?',
|
2020-03-05 13:06:47 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
checked={ requirePhoneField }
|
2020-03-05 13:06:47 +00:00
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
2020-03-16 16:38:24 +00:00
|
|
|
requirePhoneField: ! requirePhoneField,
|
2020-03-05 13:06:47 +00:00
|
|
|
} )
|
|
|
|
}
|
2020-03-16 16:38:24 +00:00
|
|
|
className="components-base-control--nested"
|
2020-03-05 13:06:47 +00:00
|
|
|
/>
|
2020-03-16 16:38:24 +00:00
|
|
|
) }
|
|
|
|
</PanelBody>
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Content', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
<p className="wc-block-checkout__controls-text">
|
|
|
|
{ __(
|
|
|
|
'Choose additional content to display.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show links to policies',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Shows a list of links to your "terms and conditions" and "privacy policy" pages.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showPolicyLinks }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showPolicyLinks: ! showPolicyLinks,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{ showPolicyLinks && ( ! PRIVACY_URL || ! TERMS_URL ) && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-base-control-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
>
|
|
|
|
{ __experimentalCreateInterpolateElement(
|
|
|
|
__(
|
|
|
|
'Pages must be first setup in store settings: <a1>Privacy policy</a1>, <a2>Terms and conditions</a2>.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
a1: (
|
|
|
|
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
|
|
|
<a
|
|
|
|
href={ getAdminLink(
|
|
|
|
'admin.php?page=wc-settings&tab=account'
|
|
|
|
) }
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
a2: (
|
|
|
|
// 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>
|
|
|
|
) }
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show a "return to cart" link',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showReturnToCart }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showReturnToCart: ! showReturnToCart,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{ showReturnToCart &&
|
2020-03-25 16:39:39 +00:00
|
|
|
( currentPostId !== CHECKOUT_PAGE_ID || !! cartPageId ) &&
|
2020-03-16 16:38:24 +00:00
|
|
|
pages && (
|
|
|
|
<SelectControl
|
2020-03-05 13:06:47 +00:00
|
|
|
label={ __(
|
2020-03-16 16:38:24 +00:00
|
|
|
'Link to',
|
2020-03-05 13:06:47 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
value={ cartPageId }
|
|
|
|
options={ [
|
|
|
|
...[
|
|
|
|
{
|
|
|
|
label: __(
|
|
|
|
'WooCommerce Cart Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
2020-03-17 10:30:52 +00:00
|
|
|
value: 0,
|
2020-03-16 16:38:24 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
...Object.values( pages ).map( ( page ) => {
|
|
|
|
return {
|
|
|
|
label: page.title.raw,
|
|
|
|
value: parseInt( page.id, 10 ),
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
] }
|
|
|
|
onChange={ ( value ) =>
|
2020-03-05 13:06:47 +00:00
|
|
|
setAttributes( {
|
2020-03-16 16:38:24 +00:00
|
|
|
cartPageId: parseInt( value, 10 ),
|
2020-03-05 13:06:47 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
</PanelBody>
|
|
|
|
<FeedbackPrompt
|
2020-03-06 11:43:40 +00:00
|
|
|
text={ __(
|
2020-03-16 16:38:24 +00:00
|
|
|
'We are currently working on improving our cart and checkout blocks, providing merchants with the tools and customization options they need.',
|
2020-03-06 11:43:40 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
/>
|
|
|
|
</InspectorControls>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const CheckoutEditor = ( { attributes, setAttributes } ) => {
|
|
|
|
const { className } = attributes;
|
|
|
|
return (
|
2020-04-07 15:41:22 +00:00
|
|
|
<EditorProvider previewCart={ previewCart }>
|
2020-03-16 16:38:24 +00:00
|
|
|
<div className={ className }>
|
|
|
|
<BlockSettings
|
2020-03-06 11:43:40 +00:00
|
|
|
attributes={ attributes }
|
2020-03-16 16:38:24 +00:00
|
|
|
setAttributes={ setAttributes }
|
2020-03-06 11:43:40 +00:00
|
|
|
/>
|
2020-03-16 16:38:24 +00:00
|
|
|
<BlockErrorBoundary
|
|
|
|
header={ __(
|
|
|
|
'Checkout Block Error',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
text={ __(
|
|
|
|
'There was an error whilst rendering the checkout block. If this problem continues, try re-creating the block.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
showErrorMessage={ true }
|
|
|
|
errorMessagePrefix={ __(
|
|
|
|
'Error message:',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-04-06 15:35:09 +00:00
|
|
|
<Disabled>
|
2020-04-07 15:41:22 +00:00
|
|
|
<Block attributes={ attributes } />
|
2020-04-06 15:35:09 +00:00
|
|
|
</Disabled>
|
2020-03-16 16:38:24 +00:00
|
|
|
</BlockErrorBoundary>
|
|
|
|
</div>
|
|
|
|
</EditorProvider>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
export default CheckoutEditor;
|