Home Forums WoodMart support forum Filter Attributes – Don't Show Out of Stock Variations

Filter Attributes – Don't Show Out of Stock Variations

Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #81773

    newsprince
    Participant

    Hello.

    I want to ask about Woodmart Woocomemrce Layered Nav widget

    For example.

    We have clothes that have the size Small, Medium, Large.

    If some Variation is out of stock for example Medium, and the customer select from filters to bring all the products that have variation Medium in size, it also brings the products with the Medium variation that are out of stock.

    Is there a way or snippet to not filter and show in results the variations of a product that is out of stock?

    Thank you.

    #81781

    Hello,

    You can hide out of stock products in Woocommerce:

    Woocommerce > Settings > Product > Inventory

    Best regards

    #81791

    newsprince
    Participant

    No the problem is related to this

    https://github.com/woocommerce/woocommerce/issues/20800

    https://github.com/woocommerce/woocommerce/issues/20689

    I guess not even an official woocommerce fix is available except maybe you develop this function to check against out of stock variations and don’t show them.

    Thank you.

    #81814

    Artem Temos
    Keymaster

    Unfortunately, we are not able to influence this functionality. It seems to be a general WooCommerce problem and we can’t fix it in our theme.

    #82017

    newsprince
    Participant

    I made a solution based on this snippet and it works

    https://github.com/ctuxboy/Woocommerce-out-of-stock-variations-loop

    add_action( 'woocommerce_before_shop_loop_item_title', 'wk_out_of_stock_variations_loop' );
    function wk_out_of_stock_variations_loop(){
        global $product;
        if ( $product->product_type == 'variable' ) { // if variation product is out of stock
            $available = $product->get_available_variations();
            if ( $available )foreach ( $available as $instockvar ) {
                if ( isset($instockvar['attributes']['attribute_pa_megethos'] ) ) {
                  
    				if ( ( $instockvar['attributes']['attribute_pa_megethos'] == $_GET['filter_megethos'] ) && (!$instockvar['max_qty']>0) ) {
                    global $product;
    		$id = $product->get_id();
    		echo "<style>.post-$id{display: none}</style>";
                    }
                
                }
            }
        }	
        if ( !$product->is_in_stock() ) { // if single product is out of stock
            		global $product;
    			$id = $product->get_id();
    			echo "<style>.post-$id{display: none}</style>";
        }
    }

    Now if you select a variation from the widget filter that is out of stock the product gets hidden from the shop results.

    #82047

    Artem Temos
    Keymaster

    Great, we are glad that you found the solution.

    #83191

    dunies
    Participant

    I have the same problem but the script doesn’t work for me.

    Xtemos this is a serious issue and you have to consider fixing it. When I choose size 40, I expect to see all shoes of available size 40. Instead I see even the out of stock ones and this is bad for the end user.

    Please give us a solution!

    #83192

    Artem Temos
    Keymaster

    Hello, @dunies,

    But it is not our theme issue and we can’t fix it. It is a problem with WooCommerce, not related to WoodMart.

    Regards

    #83483

    newsprince
    Participant

    @dunies i made a better version of the script

    I forked it from the original developer and edit it to match our need and it is working flawlesly

    Go here

    https://github.com/Moultrex/Woocommerce-out-of-stock-variations-loop/blob/master/wc-out-of-stock-variation.php

    copy the snippet and change the following 3 things

    Line 10 – 12 change the

    filter_megethos

    with the string your URL has when you apply the filter

    and change at lines

    7 – 19 – 28

    the string

    attribute_pa_megethos

    with the attribute name you have on your products

    attribute_pa_your-attribute-name

    Last change the query type to your filter widget from AND to OR

    Add the above code to functions.php file from your child theme.

    Now in our shop when you filter a size that is out of stock the results don’t show that product

    P.S.: Xtemos Team we have renewed our support period. You guys deserve it. Keep up the good work!

    Attachments:
    You must be logged in to view attached files.
    #83494

    newsprince
    Participant

    @dunies

    If you have problem editing the file post the url of your site to see the filters you have and post back the right code for your site

    #84590

    dunies
    Participant

    @newprice

    Thank you very much! This is very kind of you! I’ve just seen your messages so I’ll try it out and get back to you!

    THANK YOU SO MUCH!

    #149359

    uandi
    Participant

    @newsprince

    Thank you so much for your solution that was published on GitHub. However, the link you posted gives a 404 error. Where can I see the solution?

    By the way, this plugin (free version) gives a solution for removing out of stock variations from results: https://berocket.com/product/woocommerce-ajax-products-filter

    I would rather use WoodMart filters, but also include the above plugin solution. This is so basic & important. Why would someone want to show results of out of stock products, if the store is set to not show out of stock products…

    I really hope you can help.

    Thank you

    #149388

    newsprince
    Participant

    @uandi

    You ca find it here.

    https://pastebin.com/J4HgFdLZ

    Change attribute_pa_megethos to your attribute_pa_name-of-your-attribute

    and filter_megethos to your filter_name-of-your-attribute

    and paste it in your child theme or a snippet plugin

    #149843

    uandi
    Participant

    @newsprince

    Thank you for the link. I tried it, but it does not work. I replaced the attribute name in all 5 places with my attribute name, but I still see out of stock variations in the results. Try the size 3 for example (should have very few results): https://demostore.work/WoodMart/

    I’m including the login to my test site in the private content box.

    Thanks again

    #149913

    newsprince
    Participant

    @uandi

    Sorry i am not working for woodmart can’t see the login details.

    Try this code to your child theme and have child theme enabled.

    //Filter attributes and variations that are in stock
    
    add_action( 'woocommerce_before_shop_loop_item_title', 'wk_out_of_stock_variations_loop' );
    function wk_out_of_stock_variations_loop(){
        global $product;
        if ( $product->product_type == 'variable' ) { // if variation product is out of stock
            $available = $product->get_available_variations();
            if ( $available )foreach ( $available as $instockvar ) {
                if ( isset($instockvar['attributes']['attribute_pa_toddlers-size'] ) ) {
                  
    			  
    		if(isset($_GET['filter_toddlers-size'])){
    		   
    		   $destostock = $_GET['filter_toddlers-size'];
    			  
    		  
    		  $array = explode(',', $destostock);
    		  
    
        if (in_array($instockvar['attributes']['attribute_pa_toddlers-size'], $array )&& (!$instockvar['max_qty']>0)){
    		   global $product;
    		   $id = $product->get_id();
    	  
    			echo "<style>.post-$id{display: none}</style>";
    
    	}
    		  else{
    			
    		  if (in_array($instockvar['attributes']['attribute_pa_toddlers-size'], $array )&& ($instockvar['max_qty']>0)){
    		   global $product;
    		   $id = $product->get_id();
    	  
    			echo "<style>.post-$id{display: block !important}</style>";
    		  
    		  }
    		  }
                
            }
        }	
     
    }
    	}
    }
    #272901

    inputchip
    Participant

    @newsprince will this work if the product is set to “allow backorder” or will it get filtered out as “out of stock”?

    We have some products that take us weeks to build for our clients, they aren’t necessarily “in stock”, but sometimes we keep some styles in stock. Is it best to use the two cases “In stock” for products that are available immediately, and then “allow backorder” for those that are available to order?

Viewing 16 posts - 1 through 16 (of 16 total)