Woocomerce Back in Stock Product Help

  • 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"

kolliop

Member
Sep 24, 2020
130
16
18
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
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
843
552
93
Wherever my imagination takes me
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.
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
843
552
93
Wherever my imagination takes me
Yup, here's some code you need to put in functions.php of your child theme (or in a custom code plugin):
PHP:
You don't have permission to view the code content. Log in or register now.

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

PHP:
You don't have permission to view the code content. Log in or register now.

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

Hope this helps.
 
  • Love
Reactions: WebSpider

kolliop

Member
Sep 24, 2020
130
16
18
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 );
 

kolliop

Member
Sep 24, 2020
130
16
18
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?
 

kolliop

Member
Sep 24, 2020
130
16
18
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 ?
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
843
552
93
Wherever my imagination takes me
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.
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
843
552
93
Wherever my imagination takes me
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:
You don't have permission to view the code content. Log in or register now.

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

PHP:
You don't have permission to view the code content. Log in or register now.

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:

kolliop

Member
Sep 24, 2020
130
16
18
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.
 

Forum statistics

Threads
49,591
Messages
543,803
Members
133,956
Latest member
hyperion571
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock    No Thanks