Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › show out of stock label in results › Reply To: show out of stock label in results
December 3, 2019 at 2:09 pm
#24880
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;
}