user3389538
user3389538

Reputation: 11

Change "in stock" text on WooCommerce

Currently, on the single product page the in stock text shows up as "# in stock".

I'd like to alter the code a bit but haven't found a solution. I wasn't able to find what exact file that line of code is coming from. price.php seemed to have this

<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />

But it doesn't do anything. The class is called "stock" when I inspect element.

I tried this bit of code to add in functions.php:

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
    //change text "In Stock' to 'SPECIAL ORDER'
    if ( $_product->is_in_stock() ) $availability['availability'] = __('SPOTS LEFT', 'woocommerce');

    //change text "Out of Stock' to 'SOLD OUT'
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');
        return $availability;
    }

However, I want to be able to alter it a bit. I want to add the stock quantity in front of 'SPOTS LEFT' but I don't know where to place it. It doesn't seem like php can go inside the ('').

I tried this:

<?php echo $product->get_stock_quantity(); ?>

I want to take it up one more notch and actually have the state the number spots left only if the quantity is less than 6. Else, state sold out. Anything above 5 spots will not show. I'm not sure what the exact syntax is! Any advice would be great.

Upvotes: 1

Views: 10234

Answers (2)

AlbertMarkovski
AlbertMarkovski

Reputation: 85

This is a slight expansion on SotirisK's answer, which accounts for changing the message when the manage stock option is not chosen.

// WooCommerce Stock message

add_filter( 'woocommerce_get_availability', 'mw_get_availability', 1, 2 );

function mw_get_availability( $availability, $_product ) {

    global $product;

    // change text "In Stock' to 'SPECIAL ORDER' when quantity more than 6
    if ( $_product->is_in_stock() && $product->get_stock_quantity() > 6 ) $availability['availability'] = $product->get_stock_quantity().' '.__('Special Order', 'woocommerce');

    // change text to n Spots Left, where n is the quantity
    if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 6 ) $availability['availability'] = $product->get_stock_quantity() . __(' Spots Left);

    // change text "Out of Stock' to 'SOLD OUT'
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');

    // change text "In Stock' to 'Special Order' for products with unmanaged stock
    if ( !$_product->managing_stock() && $_product->is_in_stock() ) $availability['availability'] = __('In Stock', 'woocommerce');

    return $availability;
}

Upvotes: 1

SotirisK.
SotirisK.

Reputation: 31

// WooCommerce Stock message
add_filter( 'woocommerce_get_availability', 'mw_get_availability', 1, 2 );

function mw_get_availability( $availability, $_product ) {

//change text "In Stock' to 'SPECIAL ORDER'
global $product;
if ( $_product->is_in_stock() && $product->get_stock_quantity() < 6 ) $availability['availability'] = $product->get_stock_quantity().' '.__('SPOTS LEFT', 'woocommerce');

//change text "Out of Stock' to 'SOLD OUT'
if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');

return $availability;

}

Upvotes: 3

Related Questions