• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Woocomerce Back in Stock Product Help

kolliop

Active member
Sep 24, 2020
225
45
28
Hi to all friend,

i need some help about back in stock product in woocommerce.

i want to create one page with product grid, and i wan to display this page the last 350 product when is back to stock ,

or as new product when is again back to stock. When i add stock and some product is 0 and afet is for example 5, then this product is back in stock.

then i want to display in one page as this is my back in stock product

can help anyone for this?

i dont want Waiting List, only to display all back in stock in a page.

Thanks any way for your time
 
I don't think there's an automatic way of doing that, as it would clash with the way WooCommerce handles stock statuses. The only way this can be done - I think - is to add a custom stock status, select that while you change the inventory, and filter your products based on that status.
 
Yup, here's some code you need to put in functions.php of your child theme (or in a custom code plugin):
PHP:
// Add the extra option 'Back in Stock' to the dropdown on the product edit page
function filter_woocommerce_product_stock_status_options( $status ) {
    $status['back_in_stock'] = __( 'Back in Stock!', 'woocommerce' );
    return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );

// The text to display on the front-end (if needed)
function filter_woocommerce_get_availability_text( $availability, $product ) {
    switch( $product->get_stock_status() ) {
        case 'back_in_stock':
            $availability = __( 'Back in Stock!', 'woocommerce' );
        break;
    }
    return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

// Add a class to <li> for use in css styling (if needed)
function filter_woocommerce_get_availability_class( $class, $product ) {
    switch( $product->get_stock_status() ) {
        case 'back_in_stock':
            $class = 'back-in-stock';
        break;
    }
    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

// Display the custom status in the back-end product list
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
    switch( $product->get_stock_status() ) {
        case 'back_in_stock':
            $stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Back in Stock', 'woocommerce' ) . '</mark>';
        break;
    }   
    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );

But then of course you need a shortcode to display the Back In Stock products on a page. So, a little more code:

PHP:
// The code to add a custom attribute to woocommerce products shortcode
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ){
  if ( isset ($atts[ 'backinstock' ]) && $atts [ 'backinstock' ] ) {
    $out[ 'backinstock' ] = true;
  } else {
    $out[ 'backinstock' ] = false;     
  }
  return $out;
}
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);

// And then what the shortcode attribute 'backinstock' actually does
function handle_custom_query_var( $query, $attributes  ) {
    if ( $attributes[ 'backinstock' ] ) {
        $query['meta_query'][] = array(
            'key' => '_stock_status',
            'value' => 'back_in_stock',
            'compare' => 'IN',
        );
    }
    return $query;
}
add_filter( 'woocommerce_shortcode_products_query', 'handle_custom_query_var', 10, 2 );

And the shortcode is : [products backinstock="true"]

Hope this helps.
 
  • Love
Reactions: WebSpider
thanks a lot my bro
but this php code where i add ?
PHP:
// The code to add a custom attribute to woocommerce products shortcode
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ){
if ( isset ($atts[ 'backinstock' ]) && $atts [ 'backinstock' ] ) {
$out[ 'backinstock' ] = true;
} else {
$out[ 'backinstock' ] = false;
}
return $out;
}
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);

// And then what the shortcode attribute 'backinstock' actually does
function handle_custom_query_var( $query, $attributes ) {
if ( $attributes[ 'backinstock' ] ) {
$query['meta_query'][] = array(
'key' => '_stock_status',
'value' => 'back_in_stock',
'compare' => 'IN',
);
}
return $query;
}
add_filter( 'woocommerce_shortcode_products_query', 'handle_custom_query_var', 10, 2 );
 
hi my friend many many many thanks. it work perfect. one more think.

its possible to order and short by date?

when one product is back in stock to show fist in the list, for example when is back to stock for 0 to 10 items to short first as new produt for example

and if it is possible to show only 500 first product back in stock or if i have choice to show 100, 200, 300, 500

is about shortcode ? for that?
 
You can expand the query array with many more arguments, e.g.:

PHP:
'orderby' => 'date', //that's the date it was first published, woocommerce products shortcode does not by default include ordering by date modified, unfortunately
'order' => 'DESC',
'limit' => 300, // (the number of products you want to show in total, but if you also use the next 'pagination' argument, then it is the number of products per page)
'pagination' => true

Etc., etc. read more about the products shortcode here: https://docs.woocommerce.com/document/woocommerce-shortcodes/
 
Last edited:
  • Like
Reactions: kolliop
your help is amazing, thanks a lot,
its possibole to show only the back in stock product with orderby = the date to change the stock from 0 to ...10 for example, and not the date to puplish the product

or

if when is back to stock to for example from 0 to 20.... to change auto the date as new product ?
 
No, you cannot change the published date. As said in the code above WooCommerce doesn't include order by modified date in its 'products' shortcode, unfortunately. However there are tricks to order products by modified date, e.g. as a choice in the sorting dropdown and making that the default choice. The code for that you can find on stackoverflow: Set custom product sorting as default in WooCommerce.

But then we're having a problem on the custom page which does not have that sorting dropdown (or do you? Because then your problem is solved already).

Even without the dropdown you can make it work by adding '?orderby=modified_date' to the url, but I'm quite sure you don't want that. However, that made me thinking: well, if it works with a simple url add-on there must be a way to get that sorting by default, but how?

Do you know that feeling that something's on the tip of your tongue but you just cannot get to it? Well, I have that feeling with this one. I know I'm close, but just not exactly there, yet.
 
I knew it was something simple. Found a solution using 'pre_get_posts' function in WordPress. The code beneath is for a page with the slug 'products-back-in-stock', of course you should change that to your own page slug.

PHP:
// Sort Products by modified date when using WooCommerce's products shortcode
function backinstock_pre_get_posts( $query ) {
    // do not modify queries in the admin
    if ( is_admin() ) {
        return $query;
    }
    // only modify queries on the page where you have your shortcode [products backinstock=true]
    if( is_page('products-back-in-stock')) {
        $query->set('orderby', 'modified');
        $query->set('order', 'DESC');
    }
    return $query;
}
add_action('pre_get_posts', 'backinstock_pre_get_posts');

For the above code to work, you need to have empty values for orderby and order in your shortcode query, like so:

PHP:
            'key' => '_stock_status',
            'value' => 'back_in_stock',
            'compare' => 'IN',
            'orderby'  => '',
            'order'    => '',

I think that's needed, because of the products shortcode set-up.

If you don't want to alter the default sorting on other pages, you can just forget the code in the link in my previous post above.
 
Last edited:
  • Like
Reactions: joepremium
thanks a lot myfriend, i dont find any word to thank you, your help is amazing... and sory for my bad english....

Many many thanks, ill try and i tell you.
 
  • Like
Reactions: joepremium
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock