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 3 years, 11 months ago.
- AuthorPosts
- December 2, 2019 at 3:12 pm #24853
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 dateplan 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.
- This topic was modified 4 years, 4 months ago by skempenaar.
- This topic was modified 4 years, 4 months ago by skempenaar.
December 2, 2019 at 3:31 pm #24854or show out of stock term in the results box?
December 3, 2019 at 1:44 pm #24878like in the example.
Attachments:
You must be logged in to view attached files.December 3, 2019 at 2:09 pm #24880Hi,
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:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 3, 2019 at 2:30 pm #24881ah crap an RTFM. so sorry for this. thx!
May 20, 2020 at 7:04 pm #27471Hi 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);
}May 21, 2020 at 1:01 pm #27486Hi,
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 :)
- AuthorPosts
The topic ‘show out of stock label in results’ is closed to new replies.