0

Trying to find a way to exclude out of stock items from listing, but only after some time (let's say, if they're older than one month).

I know I can exclude all of them easily, or change the order using something like this, but I'm looking for a something in-between... I guess using a pre_get_posts hook, but not sure about the specific query.

Any tips?

1 Answer 1

2

Most information necessary can be found at the Code Reference: WP_Query | Class . Otherwise useful resource Theme Handbook: Conditional Tags. Below code contains some clarifying comments. Please note, this is untested code, so no guarantees, but it should get you going.

add_action( 'woocommerce_product_query', 'custom_woocommerce_product_query' );
function custom_woocommerce_product_query( $q ) {
  if ( ! is_admin() ) {   
    //query for the out of stock items we want to exclude
    $oos_query = new WP_Query( [ 
      // items must be older than 1 month
      'date_query' => [ [ 
        'column' => 'post_date', 
        'before' => '1 month ago' 
      ], ],
      //only out of stock items...
      'meta_query' => [ [ 
        'key' => '_stock_status',
        'value' => 'outofstock',
        'compare' => '=',
       ], ],
      //we want products, nothing else
      'post_type' => 'product',
      //we want them all
      'posts_per_page' => -1,
      //the ids are enough, no need to get more data 
      'fields' => 'ids',
    ] );
    //getting array of ids from object
    $exclude_ids = $oos_query->posts;

    //excluding the above queried products from woocommerce's product query
    $q->set( 'post__not_in', $exclude_ids );
  }
}
4
  • Thanks a lot Nicolai! query seems to work well and update the exclude ids correctly. Somehow the items are still displayed on the category listing... Not entirely sure why. I'm still in the process of debugging it. The code is executed, and I can see excluded post ids, but some of those excluded ids are still listed... :(
    – Yoav Aner
    Commented Apr 23, 2019 at 17:58
  • @YoavAner Thanks for fixing the error in the code. After peaking quickly into the source class-wc-query.php, I realized most conditionals aren't necessary, so I removed them. Maybe this resolves your issues, not 100% sure. According to the source code: »// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.«, this is where we hook in, so I think it should target category listings too. Keep me posted. Commented Apr 23, 2019 at 19:00
  • Nicolai thanks again. I realize it's nearly impossible to debug things over SO comments :) I don't think those conditions were the problem. The code was executed, and the post__not_in was set with the right ids on the query, but somehow the category page was still showing them... I'll have to spend more time debugging I guess. In any case I'll accept the answer, because it definitely seems like the right thing to do, just not sure why it's not fully working for me.
    – Yoav Aner
    Commented Apr 24, 2019 at 6:37
  • I think my problem was some weird caching issue or something. It does seem to work! One small thing however: the item counts for each category aren't affected by this update to the query, so it shows a higher count than what's actually displayed... Maybe there's a different hook to update the query on? I'll try to search and update if I find anything.
    – Yoav Aner
    Commented Apr 24, 2019 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.