Latest updates of WooCommerce had changed the way of how texts on buttons of product category listings were displayed. Text for Add to Cart buttons and texts for variable product links are easy to change with text replace code. But it was hard to find how to change default “Read More” text for products out of stock, but finally I have such snippet.
So if you want to replace any text in wordpress, just add this to your child theme functions.php
add_filter('gettext', 'translate_text');
add_filter('ngettext', 'translate_text');
function translate_text($translated) {
$translated = str_ireplace('Select Options', 'Shop Item', $translated);
$translated = str_ireplace('Fill textarea', 'Fill text', $translated);
return $translated;
}
Still, for the “Read More” button of Woocommerce you can’t use this technique, because there are could be more “read more” occurrences inside various pages, blog posts etc. So here we must be specific.
I had used the snippet from woothemes documentation, and modified it to include a link to product.
/*
* replace read more buttons for out of stock items
**/
if (!function_exists('woocommerce_template_loop_add_to_cart')) {
function woocommerce_template_loop_add_to_cart() {
global $product;
if (!$product->is_in_stock()) {
echo '<a href="'.get_permalink().'" rel="nofollow" class="outstock_button">Out of Stock</a>';
}
else
{
woocommerce_get_template('loop/add-to-cart.php');
}
}
}
So now I have a proper link to out of stock product, which I can style with my specific CSS class.
Update for Woocommerce 2.5.1
Looks like reference to woocommerce_get_template('loop/add-to-cart.php');
doesn’t supply ajax_add_to_cart
class anymore, so AJAX on Add to Cart buttons will not work.
I’ve modifyed this function with code found in plugins/woocommerce/includes/wc-template-functions.php
and it seems to work well. But may you advise a better code?
/*
* replace read more buttons for out of stock items in woo 2.5.1
**/
if (!function_exists('woocommerce_template_loop_add_to_cart')) {
function woocommerce_template_loop_add_to_cart( $args = array() ) {
global $product;
if (!$product->is_in_stock()) {
echo 'Out of Stock';
}
else
{
$defaults = array(
'quantity' => 1,
'class' => implode( ' ', array_filter( array(
'button',
'product_type_' . $product->product_type,
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : ''
) ) )
);
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
wc_get_template( 'loop/add-to-cart.php', $args );
}
}
}
Thank you, it worked!
Thanks. It worked. I spent about 30 minutes trying to figure it out. changing the language did not work.
Just what I needed, Thank you!
Great, worked like a treat
Hello, is there a way to translate this Out of Stock phrase with WPML?
It works fine but it doesn’t contain a link to the product, does it?
any update for 3.0 trying this at http://www.dannydelancey.com
Thank you!
THIS IS SO USEFUL! I used these codes for the latest version and it works! THANK YOU SO MUCH!!!
In the newest version of WordPress / Woocommerce (or at least newer than the one referred to in this post) replace `$product->product_type` with `$product->get_type`. `product_type` has been deprecated.
Sorry, it should be $product->get_type()
Thanks but on 3.0 its still not working for me?
It works great. Thanks
woocommerce is 3.3
Thank you so much, it worked for me as well, WooCommerce 4.0.1. However, with this function, the whole text box is gone as well as text formatting. Is there a way to keep the box and change the font color of ‘Out of stock’ to, say, red?
Thank you in advance.
Hi! I didn’t try to upgrade to Woocommerce 4 yet, will do it this month probably, then will publish what I found. You should change colors with CSS.