Andres Molina Perez-Tome Andres Molina Perez-Tome - 2 months ago 18
PHP Question

Customizing product variations message on single product pages

I got a WooCommerce website, which hides the prices for not logged in users, and apparently it works fine, except with product variations, that even if it does what it should, is not properly done, or at least not the way I would do it.

I hides the price of the variable product, but allows you to choose options (which is fine, that way you might encourage users to register) the problem is that when you finish choosing the variables, it shows the following message "Sorry, this product is unavailable. Please choose a different combination." Which is incorrect, is not a combination problem, but a login issue. So I wouls like to ask for some help on changing this message. Just as a quick tip, There is anoter message which i have changed already in WooCommerce with a function in the child functions.php, check the code bellow, do you think I will be able to do something similar?

function my_woocommerce_membership_notice( $message ) {
if (strpos($message,'has been removed from your cart because it can no longer be purchased') !== false) {
$message = 'An item has been removed from your cart as you have been logged out for inactivity. Please login again to add products to your cart.';
}
return $message;
}
add_filter( 'woocommerce_add_error', 'my_woocommerce_membership_notice' );


You can see the actual behaviour of the website in here: http://nataliayandres.com/oxynergy/shop/my-personalized-cream/

Thanks.

Answer

You should try to use the WordPress gettex() function that will replace the concerned message by your custom one:

add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text, $untranslated_text, $domain )
{
    if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
        $translated_text = __( 'Here goes your custom text', $domain );
    }
    return $translated_text;
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.