I have an issue I can't solve on a wordpress woocommerce website.
We chose to display the out of stock products so that our clients can see all the products and don't imagine we don't propose some of them when they're out of stock. Anyway.
I found a php snippet to hide the price of these out of stock products on categories and product pages. But I can't, for the life of me, hide the price in the suggestions that appear when one uses the live search bar in the header (ajax search).
The theme is Emallshop by PressLayout.
I've used this snippet and tried to put it in several files : functions.php, theme-function.php and woo-functions.php.
/**
* @snippet Hide Price If Out of Stock @ WooCommerce Frontend
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 6
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( 'woocommerce_get_price_html', 'bbloomer_hide_price_if_out_stock_frontend', 9999, 2 );
function bbloomer_hide_price_if_out_stock_frontend( $price, $product ) {
if ( is_admin() ) return $price; // BAIL IF BACKEND
if ( ! $product->is_in_stock() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $product );
}
return $price;
}
It works fine to hide prices of out of stock products in categories and product pages BUT NOT in the suggestions of the live search bar.
Can you help me please ? I'm no coder but I can answer questions if you ask me some about this problem ;) (and got ftp access so I can read anyfile)
My latest hint is this big chunk of code that seems to say how to get price in the suggestions... if only I knew how to transform it so that it gets price only when in stock ! Any idea ??
/* Prodcut live search
*
* @ since EmallShop 2.0
/* --------------------------------------------------------------------- */
if( ! function_exists( 'emallshop_products_live_search' ) ) :
function emallshop_products_live_search() {
$products = array();
$sku_products = array();
$products = emallshop_ajax_search_products();
$sku_products =emallshop_get_option('enable-search-by-sku', 0)
? emallshop_ajax_search_products_by_sku()
: array();
$results = array_merge( $products, $sku_products );
$suggestions = array();
foreach ( $results as $key => $post ) {
$product = wc_get_product( $post );
$product_image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ) );
$suggestions[] = array(
'id' => esc_attr( $product->get_id() ),
'value' => esc_attr( $product->get_title() ),
'url' => esc_url( $product->get_permalink() ),
'img' => esc_url( $product_image[0] ),
'price' => wp_kses_post( $product->get_price_html() ),
);
}
Thanks in advance !!! :D
Have a great day
My Answer:
To hide the price of out of stock products in the live search suggestions, you can modify the `emallshop_products_live_search` function to check if the product is in stock before displaying the price. Here's how you can do it:
php
if( ! function_exists( 'emallshop_products_live_search' ) ) :
function emallshop_products_live_search() {
$products = array();
$sku_products = array();
$products = emallshop_ajax_search_products();
$sku_products =emallshop_get_option('enable-search-by-sku', 0)
? emallshop_ajax_search_products_by_sku()
: array();
$results = array_merge( $products, $sku_products );
$suggestions = array();
foreach ( $results as $key => $post ) {
$product = wc_get_product( $post );
$product_image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ) );
if ( $product->is_in_stock() ) {
$suggestions[] = array(
'id' => esc_attr( $product->get_id() ),
'value' => esc_attr( $product->get_title() ),
'url' => esc_url( $product->get_permalink() ),
'img' => esc_url( $product_image[0] ),
'price' => wp_kses_post( $product->get_price_html() ),
);
} else {
$suggestions[] = array(
'id' => esc_attr( $product->get_id() ),
'value' => esc_attr( $product->get_title() ),
'url' => esc_url( $product->get_permalink() ),
'img' => esc_url( $product_image[0] ),
'price' => '', // Hide the price for out of stock products
);
}
}
// Rest of the function code...
}
endif;
By adding the `if ( $product->is_in_stock() )` condition inside the `foreach` loop, you can check if the product is in stock before displaying the price in the live search suggestions. If the product is out of stock, the price will be hidden.
Rate this post
3 of 5 based on 7626 votesComments