'Change the alert text on add to cart action without selected variation in Woocommerce

in Wordpress I'm using Woocommerce v3.3.5 and in single product pages for variable products, When I click the add to cart button while I don't select a variation option it popups an alert that says :

Please select some product options before adding this product to your cart.

and that is logical so far ..

My question is How to change the alert text to something else to fit my business?



Solution 1:[1]

WooCommerce has a filter "woocommerce_get_script_data" for all data sent to JavaScript including translated texts. So this is the proper way to change this text :

add_filter( 'woocommerce_get_script_data', 'change_alert_text', 10, 2 );
function change_alert_text( $params, $handle ) {
    if ( $handle === 'wc-add-to-cart-variation' )
        $params['i18n_unavailable_text'] = __( 'Your new alert text', 'domain' );

    return $params;
}

If WooCommerce change this translation, you will have a problem with the accepted answer, your condition will be false.

Solution 2:[2]

For those who cannot get the code to work, it's because the variable called by Keylies code is using the wrong variable.

The correct variable follows the same coding conventions and is incredibly adaptable.

Either you can edit public_html/YOURDOMAIN/wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php

Or drop the below code snippet (with relevant variables) into your themes functions.php file.

add_filter( 'woocommerce_get_script_data', 'change_alert_text', 10, 2 );
function change_alert_text( $params, $handle ) {
    if ( $handle === 'wc-add-to-cart-variation' )
        $params['###INSERT YOUR VARIABLE HERE###'] = __( 'Your new alert text', 'domain' );
    return $params;
}

Replace ###INSERT YOUR VARIABLE HERE### with any of the following (VARIABLE =>Default Message/Error/Text):

  • 'i18n_no_matching_variations_text' => 'Sorry, no products matched your selection. Please choose a different combination.'
  • 'i18n_make_a_selection_text' => 'Please select some product options before adding this product to your cart.'
  • 'i18n_unavailable_text' => 'Sorry, this product is unavailable. Please choose a different combination.'

You can replicate the code to replace multiple shopwide errors by using:

Custom Error 1: $params['i18n_make_a_selection_text'] = __( 'Hey, you... yeah you... make a selection!', 'domain' );

Custom Error 2: $params['i18n_unavailable_text'] = __( 'Oh no we've run out of that product but it's not the end of the world it'll be back soon', 'domain' );

Solution 3:[3]

Try this, it should solve your problem:

add_filter( 'gettext', 'customizing_variable_product_message', 97, 3 );
function customizing_variable_product_message( $translated_text, $untranslated_text, $domain )
{
    if ($untranslated_text == 'Please select some product options before adding this product to your cart.') {
        $translated_text = __( 'Here goes your custom text', $domain );
    }
    return $translated_text;
}

Code goes in function.php file of your active child theme (or active theme). It's tested and works.

Solution 4:[4]

Added to function.php this code

add_filter( 'woocommerce_get_script_data', 'change_alert_text', 10, 2 );

function change_alert_text( $params, $handle ) { if ( $handle === 'wc-add-to-cart-variation' ) $params['Please select some product options before adding this product to your cart.'] = __( 'Your new alert text', 'sivona.ee' ); return $params; }

but still: https://prnt.sc/samecx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Keylies
Solution 2 Das_Geek
Solution 3 LoicTheAztec
Solution 4 Steve