4

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.

  • Any update on this? I'm looking for a solution too – Chad Priddle Sep 07 '18 at 01:09
  • I don't wan't to write up a solution at the moment, but I hope this helps you out, if it does maybe I'll write an answer. I just developed a plugin to achieve this today, it requires the user be logged in, and provides the functionality by adding a button to the admin top bar. [Toggle Stock Plugin](https://github.com/d0n601/toggle-stock) also @ChadPriddle – Ryan Kozak May 09 '19 at 00:57

1 Answers1

0

I looked more into this and came up with a solution, it is not ideal but it does work the way that I wanted it too. I think your biggest issue is you need to store the toggle on/off into the users session so that it gets saved as they navigate it. In header.php I added:

    if (isset($_GET['hide_sold_products']) && $_GET['hide_sold_products'] === 'true') {
        WC()->session->set( 'hide_sold_products', 1 );
    }

    if (isset($_GET['hide_sold_products']) && $_GET['hide_sold_products'] === 'false') {
        WC()->session->set( 'hide_sold_products', 0 );
    }

This stores the users hide/show value from the ?hide_sold_products= url variable. Again the following is not ideal but I'll share, also added this to header.php

<?php
if($wp->request == 'store'){
            $url = $wp->request;
        } else {
            $url = strstr($wp->request, '/', true);
        }
    if(!isset($_GET['hide_sold_products'])  && $url == 'store'){
        $currentUrl = "http://" . $_SERVER['HTTP_HOST'];
        $hide_sold_products = WC()->session->get( 'hide_sold_products' );
        if($hide_sold_products == 1){
            $hide_sold_products_text = 'true';
        } else {
            $hide_sold_products_text = 'false';
        }
?>
<script>
    window.location = "<?php echo $currentUrl . '/' . $wp->request . '/?hide_sold_products=' . $hide_sold_products_text; ?>";
</script>
<?php } ?>  

This checks to see if the user is trying to access the /store/ url in woocommerce and then will redirect them and add ?hide_sold_products= with the true/false from the session value then the url calls functions.php script. Also on your toggle button/text on the front end the show toggle needs to have this appended to it: ?hide_sold_products=false

Other than the JS redirection this works well for me. I tried the wp_redirect() but ran into the headers already sent issue since the code is in the header.php

Chad Priddle
  • 650
  • 2
  • 15
  • 32