WooCommerce – Sold item Archive

It might be beneficial for search engine or for your own records. Instead of having items completely disappears out of your store, you might just want to move them out of sight – but still accessible. In my store I mainly sell vintage audio gear. Even if an item is sold, people might be keen to visit an old posting for the pictures that I have taken. As a nice side effect they might stay and buy some thats available.

All my product listing are depending on categories. To move an item away from the standard store page a category change would be sufficient. At the same time, old categories have to be wiped. Thats something not too hard to do manually but I was keen to automate that. There are plenty of plugins for Woocommerce that can help with this task. But these days almost every plugin comes with an annual subscription model and its never cheap.

To get this working your Woocommerce needs to meet so prerequisites:

  • Stock Management needs to be turned on in Settings
  • At the product in the invetory tab “Manage stock at product level” need to be turned on and a number of stock has to be provided (in my case its most of the time just 1)
  • You need to create a category “sold” or change the code accordingly
  • Your product listings use categories and filter that way through your stock

Luckily its not that hard to make these changes to Woocommerce and add  a few lines of code. All you have to do is to head over to Appearance->Theme Editor. I know it sound weird, but there is a file we can edit from the Woocommerce (WordPress) admin page the will be the receiver of the code. That file is called “functions.php”. So once you are in the Theme Editor, select that file on the right side and add the code below to the bottom of the file.

add_action( 'woocommerce_update_product', 'update_product_set_sold_cat', 10, 2 );
function update_product_set_sold_cat( $product_id, $product ) {
    if ( !$product->is_in_stock) {
        wp_set_object_terms($product_id, 'sold', 'product_cat');    
    }
}

And thats it. From here the sold items should disappear and end up in a sold categories. I have put an Archive link on my shop that lists everying in the sold categorie. That way people can easily access these items – even though its a parade of lost opportunities for a buyer.

Leave a Reply