Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Hide the shipping address form from Checkout Block in Editor and ren…
Browse files Browse the repository at this point in the history
…ame the Billing Address label when "Force shipping to the customer billing address" is enabled. (#7800)

* Rename billing address labels and hide shipping address from the editor when Force shipping to the customer billing address is enabled
  • Loading branch information
Tarun Vijwani authored Dec 2, 2022
1 parent 79e7d22 commit 1f27302
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { BlockAttributes } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import formStepAttributes from '../../form-step/attributes';
import { DEFAULT_TITLE, DEFAULT_DESCRIPTION } from './constants';

export default {
const attributes: BlockAttributes = {
...formStepAttributes( {
defaultTitle: __( 'Billing address', 'woo-gutenberg-products-block' ),
defaultDescription: __(
'Enter the billing address that matches your payment method.',
'woo-gutenberg-products-block'
),
defaultTitle: DEFAULT_TITLE,
defaultDescription: DEFAULT_DESCRIPTION,
} ),
className: {
type: 'string',
Expand All @@ -28,3 +26,4 @@ export default {
},
},
};
export default attributes;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { useMemo, useEffect, Fragment } from '@wordpress/element';
import { useMemo, useEffect, Fragment, useState } from '@wordpress/element';
import {
useCheckoutAddress,
useStoreEvents,
Expand Down Expand Up @@ -39,17 +39,36 @@ const Block = ( {
setBillingAddress,
setShippingAddress,
setBillingPhone,
setShippingPhone,
forcedBillingAddress,
} = useCheckoutAddress();
const { dispatchCheckoutEvent } = useStoreEvents();
const { isEditor } = useEditorContext();
const { forcedBillingAddress } = useCheckoutAddress();
// Clears data if fields are hidden.
useEffect( () => {
if ( ! showPhoneField ) {
setBillingPhone( '' );
}
}, [ showPhoneField, setBillingPhone ] );

const [ addressesSynced, setAddressesSynced ] = useState( false );

// Syncs shipping address with billing address if "Force shipping to the customer billing address" is enabled.
useEffect( () => {
if ( addressesSynced ) {
return;
}
if ( forcedBillingAddress ) {
setShippingAddress( billingAddress );
}
setAddressesSynced( true );
}, [
addressesSynced,
setShippingAddress,
billingAddress,
forcedBillingAddress,
] );

const addressFieldsConfig = useMemo( () => {
return {
company: {
Expand Down Expand Up @@ -77,6 +96,7 @@ const Block = ( {
setBillingAddress( values );
if ( forcedBillingAddress ) {
setShippingAddress( values );
dispatchCheckoutEvent( 'set-shipping-address' );
}
dispatchCheckoutEvent( 'set-billing-address' );
} }
Expand All @@ -97,6 +117,12 @@ const Block = ( {
dispatchCheckoutEvent( 'set-phone-number', {
step: 'billing',
} );
if ( forcedBillingAddress ) {
setShippingPhone( value );
dispatchCheckoutEvent( 'set-phone-number', {
step: 'shipping',
} );
}
} }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';

export const DEFAULT_TITLE = __(
'Billing address',
'woo-gutenberg-products-block'
);
export const DEFAULT_DESCRIPTION = __(
'Enter the billing address that matches your payment method.',
'woo-gutenberg-products-block'
);

export const DEFAULT_FORCED_BILLING_TITLE = __(
'Billing and shipping address',
'woo-gutenberg-products-block'
);
export const DEFAULT_FORCED_BILLING_DESCRIPTION = __(
'Enter the billing and shipping address that matches your payment method.',
'woo-gutenberg-products-block'
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import classnames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
import { useCheckoutAddress } from '@woocommerce/base-context/hooks';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';

import { BlockAttributes } from '@wordpress/blocks';
/**
* Internal dependencies
*/
Expand All @@ -19,6 +19,10 @@ import {
useCheckoutBlockControlsContext,
} from '../../context';
import Block from './block';
import {
getBillingAddresssBlockTitle,
getBillingAddresssBlockDescription,
} from './utils';

export const Edit = ( {
attributes,
Expand All @@ -30,7 +34,7 @@ export const Edit = ( {
showStepNumber: boolean;
className: string;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
setAttributes: ( attributes: BlockAttributes ) => void;
} ): JSX.Element | null => {
const {
showCompanyField,
Expand All @@ -41,11 +45,19 @@ export const Edit = ( {
} = useCheckoutBlockContext();
const { addressFieldControls: Controls } =
useCheckoutBlockControlsContext();
const { showBillingFields } = useCheckoutAddress();
const { showBillingFields, forcedBillingAddress } = useCheckoutAddress();

if ( ! showBillingFields ) {
if ( ! showBillingFields && ! forcedBillingAddress ) {
return null;
}
attributes.title = getBillingAddresssBlockTitle(
attributes.title,
forcedBillingAddress
);
attributes.description = getBillingAddresssBlockDescription(
attributes.description,
forcedBillingAddress
);

return (
<FormStepBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
import Block from './block';
import attributes from './attributes';
import { useCheckoutBlockContext } from '../../context';
import {
getBillingAddresssBlockTitle,
getBillingAddresssBlockDescription,
} from './utils';

const FrontendBlock = ( {
title,
Expand Down Expand Up @@ -43,7 +47,11 @@ const FrontendBlock = ( {
if ( ! showBillingFields && ! forcedBillingAddress ) {
return null;
}

title = getBillingAddresssBlockTitle( title, forcedBillingAddress );
description = getBillingAddresssBlockDescription(
description,
forcedBillingAddress
);
return (
<FormStep
id="billing-fields"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { Icon, mapMarker } from '@wordpress/icons';
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Internal dependencies
*/
import {
DEFAULT_TITLE,
DEFAULT_DESCRIPTION,
DEFAULT_FORCED_BILLING_DESCRIPTION,
DEFAULT_FORCED_BILLING_TITLE,
} from './constants';

export const getBillingAddresssBlockTitle = (
title: string,
forcedBillingAddress: boolean
): string => {
if ( forcedBillingAddress ) {
// Returns default forced billing title when forced billing address is enabled and there is no title set.
return title === DEFAULT_TITLE ? DEFAULT_FORCED_BILLING_TITLE : title;
}
// Returns default title when forced billing address is disabled and there is no title set.
return title === DEFAULT_FORCED_BILLING_TITLE ? DEFAULT_TITLE : title;
};

export const getBillingAddresssBlockDescription = (
description: string,
forcedBillingAddress: boolean
): string => {
if ( forcedBillingAddress ) {
// Returns default forced billing description when forced billing address is enabled and there is no description set.
return description === DEFAULT_DESCRIPTION
? DEFAULT_FORCED_BILLING_DESCRIPTION
: description;
}

// Returns default description when forced billing address is disabled and there is no description set.
return description === DEFAULT_FORCED_BILLING_DESCRIPTION
? DEFAULT_DESCRIPTION
: description;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import classnames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';

import { useCheckoutAddress } from '@woocommerce/base-context/hooks';
/**
* Internal dependencies
*/
Expand All @@ -30,7 +30,7 @@ export const Edit = ( {
className: string;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => {
} ): JSX.Element | null => {
const {
showCompanyField,
showApartmentField,
Expand All @@ -40,6 +40,10 @@ export const Edit = ( {
} = useCheckoutBlockContext();
const { addressFieldControls: Controls } =
useCheckoutBlockControlsContext();
const { showShippingFields } = useCheckoutAddress();
if ( ! showShippingFields ) {
return null;
}
return (
<FormStepBlock
setAttributes={ setAttributes }
Expand Down

0 comments on commit 1f27302

Please sign in to comment.