I wish to include a button on the website so that users can toggle out of stock items on and off. By default, I want the out of stock items to be false. When the user browses around I need the setting he applies to be consistent. Is this possible?
This is what I have till now:
/*
* ADDING A SIMPLE BUTTON TO SHOW OR HIDE SOLD PRODUCTS
* source: https://www.offshorly.com/news/woocommerce-show-hide-sold-products-toggle/
*/
function hide_sold_products_param() {
global $wp;
$wp->add_query_var('hide_sold_products');
}
add_filter('init', 'hide_sold_products_param');
add_action('pre_get_posts', 'hide_sold_products_query', 10);
function hide_sold_products_query($query){
if($query->get('hide_sold_products') == 'true'){
$query->set('meta_query', array(
array(
'key' => '_stock',
'value' => '0',
'compare' => '>'
)
));
}
}
I also have a button on the sidebar to toggle the status.
Currently, it is not consistent
The default is not hiding out of stock items
Any help is greatly appreciated.