Make is_checkout()=true during validation to improve compatibility

This commit is contained in:
Alex P 2023-01-30 17:33:24 +02:00
parent cd640d6441
commit 6fc12a55a9
No known key found for this signature in database
GPG key ID: 54487A734A204D71

View file

@ -32,12 +32,22 @@ class CheckoutFormValidator extends WC_Checkout {
foreach ( $data as $key => $value ) {
$_POST[ $key ] = $value;
}
// And we must call get_posted_data because it handles the shipping address.
$data = $this->get_posted_data();
// It throws some notices when checking fields etc., also from other plugins via hooks.
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@$this->validate_checkout( $data, $errors );
$is_checkout = function () {
return true;
};
// Some plugins/filters check is_checkout().
add_filter( 'woocommerce_is_checkout', $is_checkout );
try {
// And we must call get_posted_data because it handles the shipping address.
$data = $this->get_posted_data();
// It throws some notices when checking fields etc., also from other plugins via hooks.
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
@$this->validate_checkout( $data, $errors );
} finally {
remove_filter( 'woocommerce_is_checkout', $is_checkout );
}
if ( $errors->has_errors() ) {
throw new ValidationException( $errors->get_error_messages() );