skip to Main Content

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


  1. Chosen as BEST ANSWER

    I finally found a way to make it work, using the following WooCommerce Bookings filter hook (instead of using gettext hook):

    add_filter('woocommerce_bookings_calculated_booking_cost_error_output', 'modify_booking_cost_error_outout', 999, 3);
    function modify_booking_cost_error_outout( $message, $cost, $product ) {
        $error_message = $cost->get_error_message();
    
        if ( $error_message == 'Date is required - please choose one above' && has_term( 'Teaterkurser', 'product_cat', $product->get_id() ) ) {
            $message = '<span class="booking-error">Datum väljs - vänta lite...</span>';
        }
        return $message;
    }
    

  2. 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):

    if ( ! $product ) {
        wp_send_json( array(
            'result' => 'ERROR',
            'html'   => apply_filters( 'woocommerce_bookings_calculated_booking_cost_error_output', '<span class="booking-error">' . __( 'This booking is unavailable.', 'woocommerce-bookings' ) . '</span>', null, null ),
        ) );
    }
    

    And a second time at line 248:

    if ( is_wp_error( $cost ) ) {
        wp_send_json( array(
            'result' => 'ERROR',
            'html'   => apply_filters( 'woocommerce_bookings_calculated_booking_cost_error_output', '<span class="booking-error">' . $cost->get_error_message() . '</span>', $cost, $product ),
        ) );
    }
    

    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:

    add_filter('woocommerce_bookings_calculated_booking_cost_error_output', 'modify_booking_cost_error_output', 20, 3 );
    function modify_booking_cost_error_output( $message, $cost, $product ) {
        // Check that $product is defined and $cost is a WordPress Error 
        if ( ! ( is_wp_error( $cost ) && $product ) ) {
            return $message; // Exit if any is not defined
        }
        $error_message = $cost->get_error_message();
    
        if ( $error_message == 'Date is required - please choose one above' && has_term( 'Teaterkurser', 'product_cat', $product->get_id() ) ) {
            $message = '<span class="booking-error">Datum väljs - vänta lite...</span>';
        }
        return $message
    }
    

    It should avoid an error when $cost or $product are not defined.


    Now your code can be simplified using PHP str_replace() like:

    add_filter('woocommerce_bookings_calculated_booking_cost_error_output', 'modify_booking_cost_error_output', 20, 3 );
    function modify_booking_cost_error_output( $error_output, $cost, $product ) {
        if ( $product && has_term( 'Teaterkurser', 'product_cat', $product->get_id() ) ) {
            $targeted_string    = 'Date is required - please choose one above';
            $replacement_string = 'Datum väljs - vänta lite...';
            $error_output       = str_replace( $targeted_string, $replacement_string, $error_output );
        }
        return $error_output;
    }
    

    It should work in all cases without throwing any error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search