Custom Woo Messages and Text

This post will get filled out with custom php for various small changes to WooCommerce text, as well as some slightly more complex notices.

Changing Stock Status Text

In most cases the text being changed seems to be the text for backordering with notice when stock tracking is enabled. Having an extended notice on the single product page while keeping it more condensed elsewhere on the site. Use the php below to change the text. Place it inside functions.php

Please note: This php might not work as intended if the product page contains a listing that shows stock status for other products and the text on those listings should be the condensed text.

				
					function add_custom_backorder_message() {
    // Check if we are on a single product page
    if ( is_singular( 'product' ) ) {
        global $post;
        $product = wc_get_product( $post->ID );

        if ( $product && $product->is_on_backorder() ) {
            // Get the product tags
            $tags = wp_get_post_terms( $product->get_id(), 'product_tag' );

            // Check if the product has the "component" tag
            foreach ( $tags as $tag ) {
                if ( 'component' === $tag->slug ) {
                    add_filter( 'woocommerce_get_availability', 'custom_replace_backorder_message_on_single_page', 10, 2 );
                    break;
                }
            }
        }
    }
}
add_action( 'template_redirect', 'add_custom_backorder_message' );

function custom_replace_backorder_message_on_single_page( $availability, $product ) {
    // Replace the "Available on backorder" text specifically for products with the "component" tag on single product pages
    if ( $product->is_on_backorder() ) {
        $availability['availability'] = __( 'On order from our supplier. Please allow up to 10 day turnaround time.', 'woocommerce' );
    }
    return $availability;
}

function custom_replace_default_backorder_message( $availability, $product ) {
    // Replace the default "Available on backorder" text site-wide
    if ( $product->is_on_backorder() ) {
        $availability['availability'] = __( 'Available from Supplier', 'woocommerce' );
    }
    return $availability;
}
add_filter( 'woocommerce_get_availability', 'custom_replace_default_backorder_message', 10, 2 );