Skip to content

Commit e9be4e6

Browse files
committed
By default, show checkbox inline error message in block-based checkout. Use a tweak to register forced WPML strings when translating mails.
1 parent 2247700 commit e9be4e6

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

assets/js/blocks/checkout/checkout-checkboxes/checkboxes/legal-checkbox.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classnames from "classnames";
33
import { useSelect, useDispatch } from '@wordpress/data';
44
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
55
import { CheckboxControl } from '@woocommerce/blocks-checkout';
6+
import { Icon, warning } from '@wordpress/icons';
67

78
const LegalCheckbox = ({
89
checkbox,
@@ -33,7 +34,7 @@ const LegalCheckbox = ({
3334
} else if ( checkbox.is_required ) {
3435
setValidationErrors( {
3536
[ validationErrorId ]: {
36-
message: checkbox.error_message,
37+
message: checkbox.error_message ? checkbox.error_message : 'error_placeholder',
3738
hidden: true,
3839
},
3940
} );
@@ -63,6 +64,8 @@ const LegalCheckbox = ({
6364
return null;
6465
}
6566

67+
const showInlineErrorMessage = error?.hidden === false && error?.message && 'error_placeholder' !== error?.message;
68+
6669
return (
6770
<div
6871
className={ classnames(
@@ -73,6 +76,19 @@ const LegalCheckbox = ({
7376
>
7477
{ checkbox.has_checkbox ? (
7578
<>
79+
{ showInlineErrorMessage && (
80+
<div className="wc-block-components-validation-error" role="alert">
81+
<p id={ validationErrorId }>
82+
<Icon icon={ warning } />
83+
<span
84+
dangerouslySetInnerHTML={ {
85+
__html: error?.message,
86+
} }>
87+
</span>
88+
</p>
89+
</div>
90+
) }
91+
7692
<CheckboxControl
7793
key={ `checkbox-${ checkbox.id }` }
7894
{ ...fieldProps }

assets/js/blocks/checkout/checkout-checkboxes/edit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const Edit = ({ attributes, setAttributes }) => {
2727
'is_required': true,
2828
'name': 'preview',
2929
'has_checkbox': true,
30+
'error_message': '',
3031
'wrapper_classes': [],
3132
};
3233

includes/compatibility/class-wc-gzd-compatibility-wpml.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function load() {
8989
add_filter( 'wcml_get_order_items_language', array( $this, 'maybe_filter_wpml_order_items_language' ), 10, 2 );
9090
add_action( 'woocommerce_gzd_switch_email_locale', array( $this, 'setup_email_locale' ), 10, 2 );
9191
add_action( 'woocommerce_gzd_restore_email_locale', array( $this, 'restore_email_locale' ), 10, 1 );
92+
add_action( 'wpml_st_force_translate_admin_options', array( $this, 'register_forced_email_translations' ), 0 );
9293

9394
// Add compatibility with email string translation by WPML
9495
add_filter( 'wcml_emails_options_to_translate', array( $this, 'register_email_options' ), 10, 1 );
@@ -120,6 +121,37 @@ function ( $product_category ) {
120121
do_action( 'woocommerce_gzd_wpml_compatibility_loaded', $this );
121122
}
122123

124+
/**
125+
* As WCML does not provide a filter/entrypoint to detect when the plugin is translating emails
126+
* we'll need to find a tweak by checking whether WCML calls the wpml_st_force_translate_admin_options hook.
127+
* If that is the case, we'll need to register our custom strings too.
128+
*
129+
* @see WCML_Emails::force_translating_admin_options_in_backend
130+
*
131+
* @return void
132+
*/
133+
public function register_forced_email_translations() {
134+
$stack = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 5 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
135+
$is_emails = false;
136+
137+
foreach ( $stack as $backtrace ) {
138+
if ( ! isset( $backtrace['class'], $backtrace['function'] ) ) {
139+
continue;
140+
}
141+
142+
if ( 'force_translating_admin_options_in_backend' === $backtrace['function'] ) {
143+
$is_emails = true;
144+
break;
145+
}
146+
}
147+
148+
if ( $is_emails ) {
149+
remove_action( 'wpml_st_force_translate_admin_options', array( $this, 'force_admin_option_translation' ), 0 );
150+
151+
$this->force_admin_option_translation();
152+
}
153+
}
154+
123155
/**
124156
* Make sure to filter order items language in case order details or order confirmation is resent
125157
* during admin edit order requests.

src/Blocks/Checkout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ private function get_cart_data() {
486486
'label' => $checkbox->get_label(),
487487
'wrapper_classes' => array_diff( $checkbox->get_html_wrapper_classes(), array( 'validate-required', 'form-row' ) ),
488488
'custom_styles' => $checkbox->get_html_style(),
489-
'error_message' => $checkbox->get_error_message( true ),
489+
'error_message' => apply_filters( 'woocommerce_gzd_checkout_block_checkbox_show_inline_error_message', false, $checkbox ) ? $checkbox->get_error_message() : '',
490490
'html_id' => $checkbox->get_html_id(),
491491
'has_checkbox' => ! $checkbox->hide_input(),
492492
'show_for_payment_methods' => $checkbox->get_show_for_payment_methods(),

0 commit comments

Comments
 (0)