scroll-top
image

As a digital agency specialising in providing our resourcing and project management services to a wide range of agencies we are often presented with unusual requests. This one was for a high end art gallery that wanted to give its customers the option to view artwork that had already been sold, in addition to products currently on sale.

Having searched for an online solution and finding none, only others looking for a similar solution we thought we’d show you how we did this – hope it helps anyone else looking for similar functionality.

Here’s the code to be added on your current theme’s functions.php

<?php
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_status',

                'value' => 'outofstock',

                'compare' => 'NOT IN'

            )

        ));

    }

}
?>

The function “hide_sold_products_param()” is for initializing the parameter “hide_sold_products” which we will be using on the query for toggling the Hide/Show Sold Products.

The function “hide_sold_products_query” is for querying the products that do not have the “out of stock” Stock Status.

On your Woocommerce Products archive page, you will need to add a button to toggle the Hide/Show Sold Products. We did this by appending a button on top of the Sorting dropdown of the Products archive page. The code was added by overriding the default orderby.php file of Woocommerce (wp-content/plugins/woocommerce/templates/loop/orderby). You can do it by copying the contents of the orderby.php, adding the code snippet below, and putting it on your current theme (by adding a new orderby.php file in this directory: wp-content/[theme-name]/woocommerce/loop/). You can add this before the <form> element.

<?php

global $wp;

$current_url = home_url(add_query_arg(array(),$wp->request));

?>

<?php if (isset($_GET['hide_sold_products']) && $_GET['hide_sold_products'] === 'true') { ?>

    <a href="<?php echo $current_url; ?>" class="btn btn-primary">Show Sold Products</a>

<?php

} else { ?>

    <a href="<?php echo $current_url; ?>/?hide_sold_products=true" class="btn btn-primary">Hide Sold Products</a>
    
<?php } ?>

First we will need to get the current url, then we will use it to verify if it has a parameter “hide_sold_products” with the value ‘true’. If it has, then this will call the “hide_sold_products_query” function which we added on the functions.php. This will display the products that are still available (and a button “Show Sold Products” to toggle it inversely). If it doesn’t have the parameter “hide_sold_products” (meaning, this is the default url of the Products Archive page), all the products, both sold and available, will be displayed.

CATEGORIES: Technical

Author of this post

Franco

Front End Developer