questions

Hide all products with a specific stock status from WooCommerce catalog

Rate this post

I’m using Flatsome template on WordPress and Woocommerce site. I also create custom stock status (example noproduzione, used when a product is not created anymore from manufacturer). But I don’t want to show this products (noproduzione stock status) on my site (only in admin pages).

I’m using this code that I write here (I put it in my functions.php file), but it seems that on flatsome does not works. How to apply it on this template?

add_action( 'woocommerce_product_query', 'qc_action_product_query', 10, 2 );
function qc_action_product_query($q, $query) {

    // Get any existing meta query
    $meta_query = $q->get( 'meta_query');
    
    // Define an additional meta query 
    $q->set( 'meta_query', array( array(
        'key'     => '_stock_status',
        'value'   => 'noproduzione',
        'compare' => 'NOT LIKE',
    ) ) );
    
    // Set the new merged meta query
    $q->set( 'meta_query', $meta_query );
}

To create custom code I used some of codes found on StackOverflow

// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options($status) {
    // Add new statuses
    $status['10days'] = __( 'Disponibile entro 10 giorni', 'woocommerce' );
    $status['inarrivo'] = __( 'In arrivo', 'woocommerce' );
    $status['noproduzione'] = __( 'Fuori produzione', 'woocommerce' );

    return $status;
}
                  
// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text($availability, $product) {
    switch( $product->get_stock_status() ) {
        case '10days':
            $availability = __( 'Disponibile entro 10 giorni', 'woocommerce' );
        break;
        case 'inarrivo':
            $availability = __( 'In arrivo', 'woocommerce' ); 
        break;
    case 'noproduzione': 
    $availability = __( 'Fuori produzione', 'woocommerce' );  
    
        break;
    }
    return $availability;  
}

I added code to show the text of availability, and also to hide the cart button if the product is noproduzione stock status

// Display the stock status on other page
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
function wcs_stock_text_shop_page() {
    //returns an array with 2 items availability and class for CSS
    global $product;

    $availability = $product->get_stock_status();
    //check if availability in the array = string 'noproduzione'
    //if so display on page.//if you want to display the 'in stock' messages as well just leave out this, == 'Out of stock'
    if ( $product->get_stock_status() === 'noproduzione') {
        echo '<span style="color:#b20000;">Fuori produzione!</span>';
    }
    else if ( $product->get_stock_status() === 'onbackorder') {
        echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
    }
    else if ( $product->get_stock_status() === '10days') {
        echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
    }
    else if ( $product->get_stock_status() === 'inarrivo') {
        echo '<span style="color:#e0c81d;">In arrivo</span>';
    }
    else if ( $product->get_stock_status() === 'outofstock') {
        echo '<span style="color:#b20000;">Terminato!</span>';
    }
    else {
        echo '<span style="color:#53af00;">Disponibile!</span>';
    }
}

// Hide the cart button if stock status is 'noproduzione'
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback($purchasable, $product) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }
    return $purchasable;
}

Last, I also added this code, is useful because order the RELATED PRODUCT by “instock” status, and for me is good because do not show the other custom stock status. But I want to apply this to all of my frontend site.

//show, in the related products, only instock products!
add_filter( 'woocommerce_related_products', 'filter_woocommerce_related_products', 10, 3 );
function filter_woocommerce_related_products($related_posts, $product_id, $args) {    
    foreach( $related_posts as $key => $related_post ) {        
        // Get product
        $related_product = wc_get_product( $related_post );
        
        // Is a WC product 
        if ( is_a( $related_product, 'WC_Product' ) ) {
            // Stock status
            $stock_status = $related_product->get_stock_status();
            
            // NOT instock
            if ( $stock_status != 'instock' ) {
                unset( $related_posts[$key] );
            }
        }
    }
    
    return $related_posts;
}

So, the question is: how to hide this products (noproduzione stock status) on my site (and show only in admin pages)? Noproduzione is different from OUTOFSTOCK!

 

✔️Solution:

You should better use dedicated woocommerce_product_query_meta_query filter hook as follows, to hide, from your store, all products that have a specific stock status (works with custom stock status):

add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
    if ( ! is_admin() ) {
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'noproduzione',
            'compare' => '!=',
        );
    }
    return $meta_query;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works on all versions since WooCommerce 3.

Leave a Reply

Your email address will not be published.

Back to top button