Reputation: 73
In WooCommerce have been able to successfully make out of stock products to be displayed only on product category archives pages (so not in shop archive pages) with this answer code:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
// Only on shop archive pages
if( is_admin() || is_search() || ! is_shop() ) return $meta_query;
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
);
return $meta_query;
}
But I am using YITH WooCommerce Ajax Product Filter plugin, enabled and working on my shop pages.
The problem that I have now is when I want to filter out of stock products on product category archives pages, I get no filtered results and all products disappear.
I have tried to change the priority of the hook decreasing it first and then increasing it too, without any success.
I have tried adding the following filter:
add_filter ('yith_wcan_use_wp_the_query_object', '__return_true');
But it's not working either.
How can I solve this issue? Any help on this will be appreciated
Upvotes: 2
Views: 3591
Reputation: 73
I was looking around and figure out that the problem is in the thing that YITH Product Filter, when used on category page, redirects to a shop page and since the shop page has out-of-stock products excluded, it shows no results.
URL structure of Shop page without the filter:
domain.com/shop
URL structure of Shop page with applied filter:
domain.com/shop?filter_watch-gender=gender-men&query_type_watch-gender=or
URL structure of the category page that shows out-of-stock products, without applied filter:
domain.com/product-category/our-collection/
URL structure of the category page with applied filter, the same one that was applied on the Shop page:
domain.com/shop?product_cat=our-collection&source_id=25&source_tax=product_cat&filter_watch-gender=gender-men&query_type_watch-gender=or
URL structure that should be once the filter is applied on the category page:
domain.com/product-category/our-collection?filter_watch-gender=gender-men&query_type_watch-gender=or
I've tested this last URL structure on the category page with out-of-stock products, and it showed me the desired results, it has narrowed the list of products based on the applied filter.
I don't know if this can be useful to someone who might know how to help me :)
Upvotes: 0