I am using WooCommerce Bookings, that displays "Date is required – please choose one above" error notice on WooCommerce single product pages, for bookable products, when customer hasn’t chosen a mandatory booking date.
Below, I’m trying to create a function that change (or translate) this string, but only when the bookable product belongs to a specific product category:
add_filter( 'gettext', 'ead2_translate_woocommerce_strings', 999, 3 );
function ead2_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( is_product() && has_term( 'Teaterkurser', 'product_cat' ) ) {
switch ( $untranslated ) {
case 'Date is required - please choose one above':
$translated = 'Datum väljs - vänta lite...';
break;
}
}
return $translated;
}
The function doesn’t work: Something is not right and I can’t figure out what it is.
2
Answers
I finally found a way to make it work, using the following WooCommerce Bookings filter hook (instead of using
gettext
hook):Note that
woocommerce_bookings_calculated_booking_cost_error_output
filter hook, is used 2 times in includes/admin/class-wc-bookings-ajax.php file:First time at line 238 (where
$cost
and$product
arguments are null):And a second time at line 248:
So you need to check that
$product
and$cost
variables are defined in your code, to avoid a PHP error. You can use the following revised code instead:It should avoid an error when
$cost
or$product
are not defined.Now your code can be simplified using PHP
str_replace()
like:It should work in all cases without throwing any error.