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

Add validation error to prevent checkout when there is no shipping method available #8384

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
emptyHiddenAddressFields,
removeAllNotices,
} from '@woocommerce/base-utils';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch, useSelect, select as selectStore } from '@wordpress/data';
import {
CHECKOUT_STORE_KEY,
PAYMENT_STORE_KEY,
Expand Down Expand Up @@ -160,6 +160,19 @@ const CheckoutProcessor = () => {

const checkValidation = useCallback( () => {
if ( hasValidationErrors() ) {
// If there is a shipping rates validation error, return the error message to be displayed.
if (
selectStore( VALIDATION_STORE_KEY ).getValidationError(
'shipping-rates-error'
) !== undefined
) {
return {
errorMessage: __(
'Sorry, this order requires a shipping option.',
tarunvijwani marked this conversation as resolved.
Show resolved Hide resolved
'woo-gutenberg-products-block'
),
};
}
return false;
}
if ( hasPaymentError ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
} from 'wordpress-components';
import classnames from 'classnames';
import { Icon, store, shipping } from '@wordpress/icons';
import { useEffect } from '@wordpress/element';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -19,6 +22,14 @@ import type { minMaxPrices } from './shared';
import { defaultLocalPickupText, defaultShippingText } from './constants';
import { shippingAddressHasValidationErrors } from '../../../../data/cart/utils';

const SHIPPING_RATE_ERROR = {
hidden: true,
message: __(
'Shipping options are not available',
'woo-gutenberg-products-block'
),
};

const LocalPickupSelector = ( {
checked,
rate,
Expand Down Expand Up @@ -83,6 +94,23 @@ const ShippingSelector = ( {
} ) => {
const rateShouldBeHidden =
shippingCostRequiresAddress && shippingAddressHasValidationErrors();
const hasShippingPrices = rate.min !== undefined && rate.max !== undefined;
const { setValidationErrors, clearValidationError } =
useDispatch( VALIDATION_STORE_KEY );
useEffect( () => {
if ( checked === 'shipping' && ! hasShippingPrices ) {
setValidationErrors( {
'shipping-rates-error': SHIPPING_RATE_ERROR,
} );
} else {
clearValidationError( 'shipping-rates-error' );
}
}, [
checked,
clearValidationError,
hasShippingPrices,
setValidationErrors,
] );
const Price =
rate.min === undefined || rateShouldBeHidden ? (
<span className="wc-block-checkout__shipping-method-option-price">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const RatePrice = ( {
}: {
minRate: CartShippingPackageShippingRate | undefined;
maxRate: CartShippingPackageShippingRate | undefined;
multiple: boolean;
multiple?: boolean;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will fix the TypeScript error in assets/js/blocks/checkout/inner-blocks/checkout-shipping-method-block/block.tsx for not passing the multiple prop.

} ) => {
if ( minRate === undefined || maxRate === undefined ) {
return null;
Expand Down