I need to change a specific word from a translated string with variables. I'm using Spanish version of WooCommerce and don't know the exact way as it appears in the english version... but I assume, that it's not relevant to get the point.
I tried using using this kind of code snippet that works fine for a long time:
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text )
case 'Sample' :
$translated_text = __( 'Example!' );
break;}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
On WooCommerce when adding to cart a product that is exceeding the available quantity, you get the message (in spanish): "No puedes añadir esa cantidad al carrito - tenemos 9 existencias y has añadido 9 en tu carrito." (I suppose that in english is something like: "You cannot add that quantity to the cart - we have 9 in stock and you have added 9 to your cart."), where "9" is variable... and that's why I couldn't translate all the string using the gettext
filter hook.
I am trying to change the word "existencias" by the string "unidades en stock".
Any idea please?
Try the following instead using
strpos()
andstr_replace()
php functions:It could work.
For info the sentence to translate is located here in
WC_Cart
add_to_cart
method on line1203
:Related: How do I check if a string contains a specific word?