• Resolved Stu

    (@stuartiwoodheadgmailcom)


    I want to display certain products on a page but only in stock items. There seems to be some examples of adding an additional filter via PHP to do this but I can’t figure out how to implement the code (do I need to add an extra argument in my shortcake or does the PHP work site wide?

    Code…

    add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
        if( $loop_name == 'product_category' ){
            $query_args['meta_query'] = array( array(
                'key'     => '_stock_status',
                'value'   => 'outofstock',
                'compare' => 'NOT LIKE',
            ) );
        }
        return $query_args;
        }, 10, 3);

    This seems like it should be the easiest thing to do but I’m stumped

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello @stuartiwoodheadgmailcom ,

    Okay, I understand your expected result.

    The code you have shared is to modify the [products] shortcode’s output.

    If you want to hide all out-of-stock items then you have this option under wp-admin > WooCommerce > Settings > Products > Inventory section.

    If you have a plan to hide the out-of-stock items from a specific page please read through this article – https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#specific-locations

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    If you need a paid solution, you can consider taking help from one of our partner’s website.

    Thank you ??

    Thread Starter Stu

    (@stuartiwoodheadgmailcom)

    Thanks Rashed,

    When I use the above code in my function php file it does nothing to the products shortcode.

    Here is my shortcode layout:

    [products limit=”20″ columns=”4″ category=”christmas” on_sale=”true” paginate=”true”]

    I was expecting maybe to have to add an extra argument to the code i.e.

    [products limit=”20″ columns=”4″ category=”christmas” on_sale=”true” paginate=”true” Stockstatus = “instuck”]

    but you think the php should be limiting results with no additional argument?

    Just doesn’t work for me

    Hello @stuartiwoodheadgmailcom ,

    That code I suggested was for hiding out-of-stock products from all over the site.

    I have just found another way to hide out-of-stock products using the [products] shortcode. Basically, I have used your example and extended the class parameter to check if the product is in stock. Here is the code you can use in your theme’s functions.php file –

    add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop ){
    
    	if($atts['class'] == 'outofstock') {
    		$query_args['meta_query'] = array( array(
    		    'key'     => '_stock_status',
    		    'value'   => 'outofstock',
    		    'compare' => 'NOT LIKE',
    		) );
    	}
        
    
        return $query_args;
    
    }, 10, 3);

    Here is how you will use the shortcode –

    [products class="outofstock"]

    These are all complex development topics. If you have further questions about them please consult with an expert from here – https://woocommerce.com/customizations/

    Thank you ??

    Thread Starter Stu

    (@stuartiwoodheadgmailcom)

    Rashed,

    Just got chance to look at this and all I can say is a massive THANKYOU! Works like a charm. been banging my head against a wall for so long with this (maybe not seeing the wood for the trees). IF I could award you 5 stars I would. Appreciate your time and efforts.

    Awesome!

    I am glad that it was helpful. This means a lot to me.

    I am going to mark this thread as resolved. If you have any other queries, feel free to open a new thread.

    Thank you ??

    Thread Starter Stu

    (@stuartiwoodheadgmailcom)

    Rashed,

    If you fancy another challenge here is my latest problem

    https://www.ads-software.com/support/topic/set-custom-order-status-in-woocommerce-backend/

    Another ‘simple’ problem that seems to evade me!

    • This reply was modified 4 years ago by Stu.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Exclude Out Of Stock Items From [Product} Shortcode’ is closed to new replies.