Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save woogists/2d4720fe677a2034f9db71dd3aae2174 to your computer and use it in GitHub Desktop.

Select an option

Save woogists/2d4720fe677a2034f9db71dd3aae2174 to your computer and use it in GitHub Desktop.
[WooCommerce core] Use this snippet to adjust the button text for sold-out products in the product catalog view.
/**
* Change add to cart button text to "Sold Out" for out-of-stock products
* (except variable products, which keep their normal text like "Select options").
*/
function custom_sold_out_button_text( $text, $product ) {
if ( ! $product instanceof WC_Product ) {
return $text;
}
$type = $product->get_type();
// Keep default text for variable products and variations
if ( $type === 'variable' || $type === 'variation' ) {
return $text;
}
// For all other product types, if it's out of stock, label it as "Sold Out"
if ( ! $product->is_in_stock() ) {
return __( 'Sold Out', 'woocommerce' );
}
// Otherwise, keep the default text
return $text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_sold_out_button_text', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment