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-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';
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
|
|
|
import { __experimentalCreateInterpolateElement } from 'wordpress-element';
|
2020-04-22 16:44:12 +00:00
|
|
|
import { useRef } from '@wordpress/element';
|
2020-03-16 16:38:24 +00:00
|
|
|
import { EditorProvider, useEditorContext } from '@woocommerce/base-context';
|
2020-04-22 16:44:12 +00:00
|
|
|
import PageSelector from '@woocommerce/block-components/page-selector';
|
2020-04-08 15:03:39 +00:00
|
|
|
import {
|
|
|
|
previewCart,
|
2020-04-15 00:05:01 +00:00
|
|
|
previewSavedPaymentMethods,
|
2020-04-08 15:03:39 +00:00
|
|
|
checkoutBlockPreview,
|
|
|
|
} 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,
|
2020-04-21 10:51:11 +00:00
|
|
|
showApartmentField,
|
2020-03-05 13:06:47 +00:00
|
|
|
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();
|
2020-04-22 16:44:12 +00:00
|
|
|
const { current: savedCartPageId } = useRef( cartPageId );
|
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
|
2020-05-05 21:25:41 +00:00
|
|
|
title={ __(
|
|
|
|
'Address options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
>
|
|
|
|
<p className="wc-block-checkout__controls-text">
|
|
|
|
{ __(
|
2020-05-05 21:25:41 +00:00
|
|
|
'Include additional address fields in the checkout form.',
|
2020-03-16 16:38:24 +00:00
|
|
|
'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-04-21 10:51:11 +00:00
|
|
|
checked={ showApartmentField }
|
2020-03-16 16:38:24 +00:00
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
2020-04-21 10:51:11 +00:00
|
|
|
showApartmentField: ! showApartmentField,
|
2020-03-16 16:38:24 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<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
|
2020-05-05 21:25:41 +00:00
|
|
|
title={ __(
|
|
|
|
'Navigation options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-16 16:38:24 +00:00
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show links to policies',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
2020-05-05 21:25:41 +00:00
|
|
|
'Shows links to your "terms and conditions" and "privacy policy" pages.',
|
2020-03-16 16:38:24 +00:00
|
|
|
'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={ __(
|
2020-05-05 21:25:41 +00:00
|
|
|
'Show a "Return to Cart" link',
|
2020-03-16 16:38:24 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showReturnToCart }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showReturnToCart: ! showReturnToCart,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2020-04-22 16:44:12 +00:00
|
|
|
{ showReturnToCart &&
|
|
|
|
! (
|
|
|
|
currentPostId === CHECKOUT_PAGE_ID && savedCartPageId === 0
|
|
|
|
) && (
|
|
|
|
<PageSelector
|
|
|
|
pageId={ cartPageId }
|
|
|
|
setPageId={ ( id ) =>
|
|
|
|
setAttributes( { cartPageId: id } )
|
|
|
|
}
|
|
|
|
labels={ {
|
|
|
|
title: __(
|
|
|
|
'Return to Cart button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
default: __(
|
|
|
|
'WooCommerce Cart Page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
<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 } ) => {
|
2020-04-08 15:03:39 +00:00
|
|
|
const { className, isPreview } = attributes;
|
|
|
|
|
|
|
|
if ( isPreview ) {
|
|
|
|
return checkoutBlockPreview;
|
|
|
|
}
|
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
return (
|
2020-04-15 00:05:01 +00:00
|
|
|
<EditorProvider
|
|
|
|
previewData={ { previewCart, previewSavedPaymentMethods } }
|
|
|
|
>
|
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;
|