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
CodePudding user response:
You can get outofstock
products using WP_Query
and you can use wp_delete_post
to delete products.
function remove_out_of_stock_status(){
$params = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock'
)
)
);
$products = new WP_Query( $params );
if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();
wp_delete_post( get_the_ID(), false);
} wp_reset_postdata();
}
}
add_action( 'init', 'remove_out_of_stock_status', 10, 1 );
CodePudding user response:
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.
add_action('trash_outofstock_products', 'check_trash_outofstock_products');
// The action will trigger when someone visits your WordPress site
function trash_outofstock_products_activation() {
if ( !wp_next_scheduled( 'trash_outofstock_products' ) ) {
wp_schedule_event( time(), 'daily', 'trash_outofstock_products');
}
}
add_action('wp', 'trash_outofstock_products_activation');
function check_trash_outofstock_products() {
global $wpdb;
$results = $wpdb->get_results( "
SELECT p.ID, pm.meta_value as stock_status
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_stock_status'
AND pm.meta_value LIKE 'outofstock'
GROUP BY p.ID
" );
foreach($results as $result) {
wp_trash_post($result->ID);
}
}
CodePudding user response:
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.
add_action('init', 'automatically_trash_sold_items_in_woocommerce_callback');
function automatically_trash_sold_items_in_woocommerce_callback(){
// Get any existing copy of our transient data
if ( false === ( $automatically_trash_sold_items_in_woocommerce = get_transient( 'automatically_trash_sold_items_in_woocommerce' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
global $wpdb;
$wpdb->query( "UPDATE {$wpdb->posts} P JOIN {$wpdb->postmeta} PM ON P.ID = PM.post_id SET P.post_status='trash' WHERE P.post_type='product' and PM.meta_key='_stock_status' AND PM.meta_value='outofstock'");
set_transient( 'automatically_trash_sold_items_in_woocommerce', true, 12 * HOUR_IN_SECONDS );
}
}