show out of stock label in results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support show out of stock label in results

This topic contains 6 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 11 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24853
    skempenaar
    skempenaar
    Participant

    Hi,

    im kind of in a concept progress on this one so i’ve changed my mind abit arround this question.

    Best solution
    1: show out of stock label when a product isn’t instock anymore.
    2: if there are 2 books with same title show the book with the newest publishing date

    plan B:

    we have books in our store with the same name but different ISBN (sku). now most of the time these books are out of stock. i would like to hide out of stock products in my search results if the user fills in a book title. but if someone is filling in the sku of an out of stock product the result book should popup. im experimenting with the following code but this doesn’t work yet, can you help me?

    // —- Ajax SEARCH PRO – Hide out of stock—-

    if($pageposts != _sku){

    add_filter( ‘asp_pagepost_results’, ‘asp_stock_status_filter’, 1, 1 );
    }
    function asp_stock_status_filter( $pageposts ) {
    foreach ($pageposts as $k=>$v) {

    // Get the stock status
    $stock_status = get_post_meta( $v->id, ‘_stock_status’, true);

    if ( empty($stock_status) || $stock_status == “instock” )
    continue;

    unset($pageposts[$k]);
    }

    return $pageposts;
    }

    • This topic was modified 4 years, 4 months ago by skempenaar skempenaar.
    • This topic was modified 4 years, 4 months ago by skempenaar skempenaar.
    • This topic was modified 4 years, 4 months ago by skempenaar skempenaar.
    #24854
    skempenaar
    skempenaar
    Participant

    or show out of stock term in the results box?

    #24878
    skempenaar
    skempenaar
    Participant

    like in the example.

    Attachments:
    You must be logged in to view attached files.
    #24880
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Actually, you don’t need to custom code the stock status, you can use this knowledge base: https://wp-dreams.com/knowledge-base/woocommerce-showing-products-in-stock-only/

    You could also use this custom code to display a stock status message in the results content:

    add_filter( 'asp_results', 'asp_custom_field_stock', 10, 1 );  
    function asp_custom_field_stock( $results ) {
      $custom_field = "_stock_status";
     
      foreach ($results as $k=>&$r) {
        if (
            $r->content_type != "pagepost" || 
            $r->post_type != 'product' ||
            $r->post_type != 'product_variation'
          ) continue;
        
          $value = get_post_meta( $r->id, $custom_field, true );
          if ( $value == 'instock' ) {
            $r->content  .= '<br>In Stock';
          } else {
            $r->content  .= '<br>Out of Stock';
          }
      }
      
      return $results;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #24881
    skempenaar
    skempenaar
    Participant

    ah crap an RTFM. so sorry for this. thx!

    #27471
    skempenaar
    skempenaar
    Participant

    Hi ernst,

    quick question about the following code. we have products in backorder that
    load below the products that are instock. i would like it to function only if it is out of stock.

    // —- Ajax SEARCH PRO -put to bottom —-

    add_filter(‘asp_results’, ‘asp_order_out_of_scock’, 10, 1);
    function asp_order_out_of_scock($results) {
    $first = array();
    $last = array();
    foreach ( $results as $k => &$r ) {
    if ( get_post_meta($r->id, ‘_stock_status’, true) == ‘instock’ ) {
    $first[] = $r;
    } else {
    $last[] = $r;
    }
    }
    return array_merge($first, $last);
    }

    #27486
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    In this tutorial you will find how to configure that, or there is also a custom code solution, if you prefer that.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘show out of stock label in results’ is closed to new replies.