The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Changing 'Out of stock' to 'coming soon& (37 posts)

  1. chetan0412
    Posted 3 years ago #



    but its working on my local server not on my live site...

  2. cokeyblokey
    Posted 3 years ago #

    I've run into a small problem changing this message when using html tags within the str_ireplace. I'm using the following code in my themes functions.php file:

    add_filter( 'woocommerce_get_availability', 'change_out_of_stock' );
    function change_out_of_stock($availability)
    $availability['availability'] = str_ireplace( 'Out of stock', 'To order call our sales team on <br><span class="order"> 01428 608436 </span>', $availability['availability']);
    return $availability;

    It works fine for a Variable product but for Simple products the html tags get output'd sent to the browser as special characters, with the consequence being the browser just prints the tags. For instance the tag:
    would shown in the source code as:
    & l t ; br/ & g t ;
    (sorry about the spaces but it's the only way to show it without the forum converting it to a tag...
    Like i say, it's fine for Variable products but not for Simple products

    Any ideas, anyone?

  3. cokeyblokey
    Posted 3 years ago #

    After a few hours of investigation, I've got the solution to allowing HTML within the replaced 'out of stock' message for Simple Products...

    The Simple Products template file was using the 'esc_html' function for the 'out of stock' message. This escapes any HTML characters when they are sent to the browser.

    To edit this in the correct way, you need to switch the 'esc_html' function with 'wp_kses' and then give an array of allowed HTML.

    First off, copy the file:
    to your theme folder:
    (This ensures the changes you are about to make are not lost when the plugin is updated).

    Now change line 27 of the copied file, from:

    echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</p>', $availability['availability'] );


    echo apply_filters( 'woocommerce_stock_html', '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . wp_kses($availability['availability'], $allowed_html) . '</p>', $availability['availability'] );
    $allowed_html = array(
    'br' => array(),
    'span' => array(
    'class' => array()

    The '$allowed_html' array will need to contain the correct html for your situation (notice that the 'class' array is within the 'span' array - just as it needs to be in the HTML markup).

    I've even verified with Woocommerce Support that the above code change is safe. Their reply:

    Yes you can do it, there's no security problem here.

    I hope this little snippet saves somebody some time!

  4. marcods
    Posted 2 years ago #

    @terrytsang i tried to use your code but.. is it possible (how?) to have a choice in the admin between sold out and out of stock?


  5. terrytsang
    Posted 2 years ago #

    @cokeyblokey, thank for the tips :)

  6. terrytsang
    Posted 2 years ago #

    @marcods, the admin option you will need to develop that into a plugin where you have a new field for each product liked "Custom Out of Stock Text", and have a default one if that is not set. Hope my explanation give you some idea.

  7. marcods
    Posted 2 years ago #

    mmm. sort of. i'll try to follow your kind suggestion!

    thanks. best regards

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.