docs/block-development/extensible-blocks/cart-and-checkout-blocks/checkout-utilities.md
Utility functions and React hooks for checkout functionality, available from @woocommerce/blocks-checkout.
useValidateCheckoutA hook that validates the checkout form and automatically scrolls to the first validation error if any are found.
This hook is primarily used internally by the PlaceOrderButton component to provide the validate prop to custom place order button components. However, it can also be used directly if needed.
// Aliased import
import { useValidateCheckout } from '@woocommerce/blocks-checkout';
// Global import
// const { useValidateCheckout } = wc.blocksCheckout;
const MyComponent = () => {
const validateCheckout = useValidateCheckout();
const handleClick = async () => {
const { hasError } = await validateCheckout();
if ( hasError ) {
// Validation failed - errors are automatically shown and
// the page scrolls to the first error
return;
}
// Validation passed - proceed with your logic
};
return <button onClick={ handleClick }>Validate</button>;
};
The hook returns a function that, when called:
CHECKOUT_VALIDATION event to run all registered validation callbacks{ hasError: boolean }| Property | Type | Description |
|---|---|---|
hasError | boolean | true if validation failed, false if passed. |