Is there a way to automatically trash sold items (out of stock) in WooCommerce at the end of the day? For example at midnight?
As many company marketing the products and the out of stock items/sold items are also being advertised. Company cannot manage to take them out of the advertisements.
In this case, we need to run a script that move out of stock product to the trash.
As we have thousand of products, so we need a code snippet that is faster in execution
3
Answers
For this question,
I have done analysis and found that going with core query make the execution faster compared to WP_Query
I have found that just changing the post_status for out of stock product to 'trash' will do the work.
Below code execute every 12 Hours provided site is in action. You can change the time frame as per your need.
Put below code snippets in functions.php of active theme.
You can delete
outofstock
products usingget_results
.You will need cron job to automate it. You can always install WP Control plugin from which you can run event when you need. My solution is with sql request collecting product ids that are out of stock. I think performance wise this is the fastest way.