This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

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

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24853
    skempenaarskempenaar
    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 6 years, 6 months ago by skempenaarskempenaar.
    • This topic was modified 6 years, 6 months ago by skempenaarskempenaar.
    • This topic was modified 6 years, 6 months ago by skempenaarskempenaar.
    #24854
    skempenaarskempenaar
    Participant

    or show out of stock term in the results box?

    #24878
    skempenaarskempenaar
    Participant

    like in the example.

    #24880
    Ernest MarcinkoErnest 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;
    }
    #24881
    skempenaarskempenaar
    Participant

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

    #27471
    skempenaarskempenaar
    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 MarcinkoErnest 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.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘show out of stock label in results’ is closed to new replies.