WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Changing 'Out of stock' to 'coming soon& (37 posts)

  1. chetan0412
    Member
    Posted 1 year ago #

    @terrytsang

    Superb

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

  2. cokeyblokey
    Member
    Posted 1 year 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:
    <br/>
    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
    Member
    Posted 1 year 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:
    'plugins/woocommerce/templates/single-product/add-to-cart/simple.php'
    to your theme folder:
    'themes/your_theme_name/woocommerce/single-product/add-to-cart/simple.php'
    (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'] );

    to:

    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
    Member
    Posted 10 months 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?

    thanks!

  5. terrytsang
    Member
    Posted 10 months ago #

    @cokeyblokey, thank for the tips :)

  6. terrytsang
    Member
    Posted 10 months 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
    Member
    Posted 10 months 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

About this Topic

Tags

No tags yet.