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

Remove WC Core shipping settings if Cart/Checkout blocks are in use #8679

Merged
merged 58 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
f60bc6a
Add CartCheckoutUtils class
opr Mar 7, 2023
9b75889
Update ShippingController to use the new CartCheckoutUtils function
opr Mar 7, 2023
cdb1c99
Add filter to remove shipping settings when Cart/Checkout are default
opr Mar 7, 2023
50a0218
Ensure setting displays correctly if cart is default but not checkout
opr Mar 9, 2023
10702bb
Add tests to ensure core shipping settings update correctly
opr Mar 9, 2023
a3ea8f8
Add setCartCheckoutPages function to update set the cart/checkout page
opr Mar 7, 2023
b41c1e6
Force shipping to be enabled if the Checkout block is in use.
opr Mar 7, 2023
1c6b0be
Add filter to override cost requires address option
opr Mar 7, 2023
37a5ae3
Add shippingCostRequiresAddress option
opr Mar 7, 2023
0ac41c8
Check if the address is required before showing rates
opr Mar 7, 2023
90ba15a
Show shipping rates in editor
opr Mar 7, 2023
d860856
Add shippingCostRequiresAddress attribute to shipping methods block
opr Mar 7, 2023
ca5b32e
Update frontend type to show shippingCostRequiresAddress is a prop
opr Mar 7, 2023
8ea5da9
Add control to toggle shippingCostRequiresAddress option
opr Mar 7, 2023
13c0455
Show address notice in the correct scenario
opr Mar 7, 2023
ec6fdb1
Send shippingCostRequiresAddress to Block in front end context
opr Mar 7, 2023
4573079
Add e2e test for editor control
opr Mar 8, 2023
7dd98e2
Add e2e tests for shipping options on the front end
opr Mar 8, 2023
4a06ea2
Add updateAttributeInSiblingBlock function
opr Mar 8, 2023
03ebef7
Add shippingCostRequiresAddress to shipping method block
opr Mar 8, 2023
3a0a0f5
Ensure attribute is updated in both blocks when editing
opr Mar 8, 2023
caf1d86
In Shipping Methods Block, show correct component based on block setting
opr Mar 8, 2023
38c93be
Show correct block in editor
opr Mar 9, 2023
5d70625
Remove broken test from PR
opr Mar 9, 2023
795c8ed
Clean up updateAttributeInSiblingBlock
opr Mar 9, 2023
baf89ca
Add setCartCheckoutPages function to update set the cart/checkout page
opr Mar 7, 2023
a491f33
Add tests to ensure core shipping settings update correctly
opr Mar 7, 2023
283c15d
Add isAddressComplete function
opr Mar 7, 2023
909d5cc
Check if the address is required before showing rates
opr Mar 7, 2023
384f620
Show shipping rates in editor
opr Mar 7, 2023
faeda40
Show address notice in the correct scenario
opr Mar 7, 2023
4a7665b
Add e2e tests for shipping options on the front end
opr Mar 8, 2023
4c6aeb6
Ensure errorId is passed to StateInput
opr Mar 8, 2023
9da6145
Add fullShippingAddressPushed action to wc/store/cart
opr Mar 8, 2023
ea9af41
Add fullShippingAddressPushed case to reducer
opr Mar 8, 2023
51f259f
Ensure fullShippingAddressPushed is set when initialising cart store
opr Mar 8, 2023
153e77b
Add fullShippingAddressPushed selector and default state entry
opr Mar 8, 2023
33b981b
Add shippingAddressHasValidationErrors util function
opr Mar 8, 2023
043b0f0
Do not overwrite addresses when selecting a rate
opr Mar 8, 2023
9c9e410
Set whether full address has been pushed when saving address changes
opr Mar 8, 2023
85995d6
In Shipping Methods Block, show correct component based on block setting
opr Mar 8, 2023
6458028
Don't show from price if rates should be hidden until address entered
opr Mar 8, 2023
8fd650f
Check city validation errors to assert if shipping address is valid
opr Mar 8, 2023
1dec79e
Rename merchant.js to merchant.ts
opr Mar 8, 2023
289d0c9
Move local pickup functions to common merchant util
opr Mar 8, 2023
707e326
Update local pickup tests to use common merchant utils
opr Mar 8, 2023
611724a
Add test to ensure setting toggles in both blocks
opr Mar 8, 2023
11ffd8c
Add navigating to settings and saving in merchant util
opr Mar 8, 2023
7c82937
Create addPickupLocation merchant util
opr Mar 8, 2023
354c6eb
Add test for local pickup and require full address
opr Mar 8, 2023
c70cff8
Make sure correct conditions are met to show shipping options
opr Mar 9, 2023
351f2d9
Ensure checkbox is checked during local pickup tests
opr Mar 9, 2023
26b2f2c
Unset the checkbox when tests are finished running
opr Mar 9, 2023
a9b934d
Update checkout block fixture
opr Mar 9, 2023
b494750
Prevent error in unit tests
opr Mar 9, 2023
89c2bb0
Import validation store key from constants
opr Mar 9, 2023
a33ed88
Update checkout terms test to wait for button not to be disabled
opr Mar 9, 2023
54833ea
Revert "Add isAddressComplete function"
opr Mar 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/js/base/components/state-input/StateInputProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface StateInputProps {
onChange: ( value: string ) => void;
required?: boolean;
errorMessage?: string;
errorId?: string;
}

export type StateInputWithStatesProps = StateInputProps & {
Expand Down
2 changes: 2 additions & 0 deletions assets/js/base/components/state-input/state-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const StateInput = ( {
autoComplete = 'off',
value = '',
required = false,
errorId = '',
}: StateInputWithStatesProps ): JSX.Element => {
const countryStates = states[ country ];
const options = useMemo(
Expand Down Expand Up @@ -102,6 +103,7 @@ const StateInput = ( {
'Please select a state.',
'woo-gutenberg-products-block'
) }
errorId={ errorId }
required={ required }
autoComplete={ autoComplete }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ export default {
remove: true,
},
},
shippingCostRequiresAddress: {
type: 'boolean',
default: false,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"remove": true,
"move": true
}
},
"shippingCostRequiresAddress": {
"type": "boolean",
"default": false
}
},
"parent": [ "woocommerce/checkout-fields-block" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import './style.scss';
import { RatePrice, getLocalPickupPrices, getShippingPrices } from './shared';
import type { minMaxPrices } from './shared';
import { defaultLocalPickupText, defaultShippingText } from './constants';
import { shippingAddressHasValidationErrors } from '../../../../data/cart/utils';

const LocalPickupSelector = ( {
checked,
Expand Down Expand Up @@ -71,15 +72,19 @@ const ShippingSelector = ( {
showPrice,
showIcon,
toggleText,
shippingCostRequiresAddress = false,
}: {
checked: string;
rate: minMaxPrices;
showPrice: boolean;
showIcon: boolean;
shippingCostRequiresAddress: boolean;
toggleText: string;
} ) => {
const rateShouldBeHidden =
shippingCostRequiresAddress && shippingAddressHasValidationErrors();
const Price =
rate.min === undefined ? (
rate.min === undefined || rateShouldBeHidden ? (
<span className="wc-block-checkout__shipping-method-option-price">
{ __(
'calculated with an address',
Expand Down Expand Up @@ -122,11 +127,13 @@ const Block = ( {
showIcon,
localPickupText,
shippingText,
shippingCostRequiresAddress = false,
}: {
checked: string;
onChange: ( value: string ) => void;
showPrice: boolean;
showIcon: boolean;
shippingCostRequiresAddress: boolean;
localPickupText: string;
shippingText: string;
} ): JSX.Element | null => {
Expand All @@ -145,6 +152,7 @@ const Block = ( {
rate={ getShippingPrices( shippingRates[ 0 ]?.shipping_rates ) }
showPrice={ showPrice }
showIcon={ showIcon }
shippingCostRequiresAddress={ shippingCostRequiresAddress }
toggleText={ shippingText || defaultShippingText }
/>
<LocalPickupSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { innerBlockAreas } from '@woocommerce/blocks-checkout';
import { useDispatch, useSelect } from '@wordpress/data';
import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
import ExternalLinkCard from '@woocommerce/editor-components/external-link-card';
import { Attributes } from '@woocommerce/blocks/checkout/types';
import { updateAttributeInSiblingBlock } from '@woocommerce/utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -152,7 +154,9 @@ const ShippingSelector = ( {
export const Edit = ( {
attributes,
setAttributes,
clientId,
}: {
clientId: string;
attributes: {
title: string;
description: string;
Expand All @@ -163,9 +167,16 @@ export const Edit = ( {
showPrice: boolean;
showIcon: boolean;
className: string;
shippingCostRequiresAddress: boolean;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element | null => {
const toggleAttribute = ( key: keyof Attributes ): void => {
const newAttributes = {} as Partial< Attributes >;
newAttributes[ key ] = ! ( attributes[ key ] as boolean );
setAttributes( newAttributes );
};

const { setPrefersCollection } = useDispatch( CHECKOUT_STORE_KEY );
const { prefersCollection } = useSelect( ( select ) => {
const checkoutStore = select( CHECKOUT_STORE_KEY );
Expand Down Expand Up @@ -210,6 +221,30 @@ export const Edit = ( {
) }
>
<InspectorControls>
<PanelBody
title={ __(
'Calculations',
'woo-gutenberg-products-block'
) }
>
<ToggleControl
label={ __(
'Hide shipping costs until an address is entered',
'woo-gutenberg-products-block'
) }
checked={ attributes.shippingCostRequiresAddress }
onChange={ ( selected ) => {
updateAttributeInSiblingBlock(
clientId,
'shippingCostRequiresAddress',
selected,
'woocommerce/checkout-shipping-methods-block'
);

toggleAttribute( 'shippingCostRequiresAddress' );
} }
/>
</PanelBody>
<PanelBody
title={ __( 'Appearance', 'woo-gutenberg-products-block' ) }
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ const FrontendBlock = ( {
showIcon,
shippingText,
localPickupText,
shippingCostRequiresAddress,
}: {
title: string;
description: string;
showStepNumber: boolean;
shippingCostRequiresAddress: boolean;
children: JSX.Element;
className?: string;
showPrice: boolean;
Expand Down Expand Up @@ -90,6 +92,7 @@ const FrontendBlock = ( {
showIcon={ showIcon }
localPickupText={ localPickupText }
shippingText={ shippingText }
shippingCostRequiresAddress={ shippingCostRequiresAddress }
/>
{ children }
</FormStep>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export default {
remove: true,
},
},
shippingCostRequiresAddress: {
type: 'boolean',
default: false,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"remove": true,
"move": true
}
},
"shippingCostRequiresAddress": {
"type": "boolean",
"default": false
}
},
"parent": [ "woocommerce/checkout-fields-block" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import type {
CartShippingPackageShippingRate,
} from '@woocommerce/types';
import type { ReactElement } from 'react';
import { useSelect } from '@wordpress/data';
import { CART_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
*/
import './style.scss';
import { shippingAddressHasValidationErrors } from '../../../../data/cart/utils';

/**
* Renders a shipping rate control option.
Expand All @@ -52,7 +55,10 @@ const renderShippingRatesControlOption = (
};
};

const Block = ( { noShippingPlaceholder = null } ): ReactElement | null => {
const Block = ( {
noShippingPlaceholder = null,
shippingCostRequiresAddress = false,
} ): ReactElement | null => {
const { isEditor } = useEditorContext();

const {
Expand All @@ -63,6 +69,10 @@ const Block = ( { noShippingPlaceholder = null } ): ReactElement | null => {
isCollectable,
} = useShippingData();

const shippingAddressPushed = useSelect( ( select ) => {
return select( CART_STORE_KEY ).getFullShippingAddressPushed();
} );

const filteredShippingRates = isCollectable
? shippingRates.map( ( shippingRatesPackage ) => {
return {
Expand All @@ -81,13 +91,15 @@ const Block = ( { noShippingPlaceholder = null } ): ReactElement | null => {
return null;
}

const shippingAddressIsComplete = ! shippingAddressHasValidationErrors();

const shippingRatesPackageCount =
getShippingRatesPackageCount( shippingRates );

if (
! isEditor &&
! hasCalculatedShipping &&
! shippingRatesPackageCount
( ! hasCalculatedShipping && ! shippingRatesPackageCount ) ||
( shippingCostRequiresAddress &&
( ! shippingAddressPushed || ! shippingAddressIsComplete ) )
) {
return (
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, ExternalLink } from '@wordpress/components';
import { PanelBody, ExternalLink, ToggleControl } from '@wordpress/components';
import { ADMIN_URL, getSetting } from '@woocommerce/settings';
import ExternalLinkCard from '@woocommerce/editor-components/external-link-card';
import { innerBlockAreas } from '@woocommerce/blocks-checkout';
import { useCheckoutAddress } from '@woocommerce/base-context/hooks';
import Noninteractive from '@woocommerce/base-components/noninteractive';
import { Attributes } from '@woocommerce/blocks/checkout/types';
import { updateAttributeInSiblingBlock } from '@woocommerce/utils';

/**
* Internal dependencies
Expand All @@ -32,12 +34,15 @@ type shippingAdminLink = {
export const Edit = ( {
attributes,
setAttributes,
clientId,
}: {
clientId: string;
attributes: {
title: string;
description: string;
showStepNumber: boolean;
className: string;
shippingCostRequiresAddress: boolean;
};
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element | null => {
Expand All @@ -54,6 +59,12 @@ export const Edit = ( {
return null;
}

const toggleAttribute = ( key: keyof Attributes ): void => {
const newAttributes = {} as Partial< Attributes >;
newAttributes[ key ] = ! ( attributes[ key ] as boolean );
setAttributes( newAttributes );
};

return (
<FormStepBlock
attributes={ attributes }
Expand All @@ -64,6 +75,29 @@ export const Edit = ( {
) }
>
<InspectorControls>
<PanelBody
title={ __(
'Calculations',
'woo-gutenberg-products-block'
) }
>
<ToggleControl
label={ __(
'Hide shipping costs until an address is entered',
'woo-gutenberg-products-block'
) }
checked={ attributes.shippingCostRequiresAddress }
onChange={ ( selected ) => {
updateAttributeInSiblingBlock(
clientId,
'shippingCostRequiresAddress',
selected,
'woocommerce/checkout-shipping-method-block'
);
toggleAttribute( 'shippingCostRequiresAddress' );
} }
/>
</PanelBody>
{ globalShippingMethods.length > 0 && (
<PanelBody
title={ __(
Expand Down Expand Up @@ -129,7 +163,12 @@ export const Edit = ( {
) }
</InspectorControls>
<Noninteractive>
<Block noShippingPlaceholder={ <NoShippingPlaceholder /> } />
<Block
noShippingPlaceholder={ <NoShippingPlaceholder /> }
shippingCostRequiresAddress={
attributes.shippingCostRequiresAddress
}
/>
</Noninteractive>
<AdditionalFields block={ innerBlockAreas.SHIPPING_METHODS } />
</FormStepBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const FrontendBlock = ( {
showStepNumber,
children,
className,
shippingCostRequiresAddress = false,
}: {
title: string;
description: string;
Expand All @@ -31,6 +32,7 @@ const FrontendBlock = ( {
showStepNumber: boolean;
children: JSX.Element;
className?: string;
shippingCostRequiresAddress: boolean;
} ) => {
const checkoutIsProcessing = useSelect( ( select ) =>
select( CHECKOUT_STORE_KEY ).isProcessing()
Expand All @@ -53,7 +55,9 @@ const FrontendBlock = ( {
description={ description }
showStepNumber={ showStepNumber }
>
<Block />
<Block
shippingCostRequiresAddress={ shippingCostRequiresAddress }
/>
{ children }
</FormStep>
);
Expand Down
1 change: 1 addition & 0 deletions assets/js/data/cart/action-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const ACTION_TYPES = {
SET_CART_DATA: 'SET_CART_DATA',
SET_FULL_SHIPPING_ADDRESS_PUSHED: 'SET_FULL_SHIPPING_ADDRESS_PUSHED',
SET_ERROR_DATA: 'SET_ERROR_DATA',
APPLYING_COUPON: 'APPLYING_COUPON',
REMOVING_COUPON: 'REMOVING_COUPON',
Expand Down
Loading