skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 );
        }
    }
    

  2. You can delete outofstock products using get_results.

    function remove_out_of_stock_status(){
        
        global $wpdb;
    
        $results = $wpdb->get_results( "
            DELETE p FROM {$wpdb->prefix}posts p 
            join {$wpdb->prefix}postmeta pm 
            on p.ID = pm.post_id 
            WHERE p.post_type = 'product' 
            and pm.meta_key = '_stock_status' 
            and pm.meta_value = 'outofstock'
        " );
    
    }
    
    add_action( 'init', 'remove_out_of_stock_status', 10, 1 );
    
    Login or Signup to reply.
  3. 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);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search