• Resolved madmax4ever

    (@madmax4ever)


    Hello,
    I have mainly unique products. So I don’t want to show out of stock products.
    I wrote a function, modifying pre_get_posts to hide those when searching inside my web site.
    add_action( 'pre_get_posts', 'my_hide_out_of_stock_products' );
    I works well this way, but it seems that Woocommerce Product Filter works differently. So I choose to add the “Stock Status” filter, choose “In stock” as default value and hide this filter.
    But at page first load, no (pre)filter is applied. If I select something in a filter, it works well: pr_stock=instock is added to the query, and outofstock items are not shown.

    But still: at first page load, in my “Size” filter, I have option (say it is “X”) that lead to “No product found” and disappearance of this X option from the filter when removing the selected size filter.
    What is weird is that without instock filter, the filter behaves the same: I check “X”, it filters, no product found, I remove “X”, filters change and now the “X” option isn’t shown anymore (this is great but why was the “X” option shown in the first place”?)?

    Do you have a wordpress filter or could you had and option to chosse if outofstock items are to be considered or not?
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter madmax4ever

    (@madmax4ever)

    I forgot to say that Remove actions before ajax filtering is not set, not to remove my action added to pre_get_posts

    Thread Starter madmax4ever

    (@madmax4ever)

    Never mind my pre_get_posts function: I modified it to contain both:

    // Exclude out of stock products
    $meta_query[] = array(
    	'key'       => '_stock_status',
    	'value'     => 'outofstock',
    	'compare'   => 'NOT IN'
    );

    and

    $tax_query[] = array(
    	'taxonomy' => 'product_visibility',
    	'field' => 'term_taxonomy_id',
    	'terms' => array( $outofstock_term->term_taxonomy_id ),
    	'operator' => 'NOT IN'
    );

    I was previously just excluding using tax_query… Now I get what I want.

    But to me, a filter value set as “default”, should be filtered at first load… What is the purpose of “default” otherwise?

    Plugin Author Nick McReynolds

    (@woobewoo)

    Hello @madmax4ever,

    We will make it so that the values set by default are applied at the first page load to the selection of products.

    “pre_get_posts does not fire” – Please contact our internal support team.
    https://woobewoo.com/contact-us/

    What is the full query and where does it go to get only IN STOCK product (or product variations) to show when filtering?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘In Stock as default, but not applied at page load’ is closed to new replies.