Support » Plugin: WooCommerce » hide out of stock variation product from woocommerce shortcode loop by term

  • Resolved divya275

    (@divya275)


    Hello gays,

    I want to remove out of stock product from product loop. I have display product loop on custom page using woocommerce shortcode [products columns=”4″ attribute=”style” terms=”style-2″ orderby=”popularity” paginate=”true” limit=”20″].

    how to remove style-2 term variation product from product loop if it is out of stock.

    Any hook is available to remove the product from the product loop.

    eagerly Waiting for your positive response.

    Thanks

Viewing 1 replies (of 1 total)
  • Plugin Support Mike W

    (@nixiack8)

    Hi @divya275,

    I want to remove out of stock product from product loop.

    You can do this overall with this snippet that you can put in the functions.php file of your Child theme:

    add_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
    
    function iconic_hide_out_of_stock_products( $q ) {
    
        if ( ! $q->is_main_query() || is_admin() ) {
            return;
        }
    
        if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
    
            $tax_query = (array) $q->get('tax_query');
    
            $tax_query[] = array(
                'taxonomy' => 'product_visibility',
                'field' => 'term_taxonomy_id',
                'terms' => array( $outofstock_term->term_taxonomy_id ),
                'operator' => 'NOT IN'
            );
    
            $q->set( 'tax_query', $tax_query );
    
        }
    
        remove_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );
    
    }

    Product visibility is now handled in the form of a taxonomy, rather than post meta. So the above snippet will show all products as long as they are not set to outofstock. This includes product variations, so it should work for the shortcodes as well.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.