How to hide out of stock products after 10 days "out of stock" on WooCommerce shop page

Viewed 585

I'm trying to develop a snippet in order to hide out-of-stock products after 10 days when they went sold out.

I'm trying to develop a snippet to hide out-of-stock products after 10 days when they sold out.

For that I was inspired by:

I have to build a custom function which will check the out of stock date. But I do not find any value or properties, which stores that date and time on which the product was marked as sold out.

Is there something out of the box available or do I have to create a snipped first to store the date and time when a product is marked as sold out and use that info with a IF statement to hide a product or not based on the days in between.

add_action('woocommerce_product_query', 'custom_woocommerce_product_query');

function custom_woocommerce_product_query($q)
    {
       if ( out_of_stock < 10 ())
           {
            $oos_query = new WP_Query(['meta_query' => [['key' => '_stock_status', 'value' => 'outofstock', 'compare' => '=', ], ], 'post_type' => 'product', 'posts_per_page' => - 1, 'fields' => 'ids', ]);
            $exclude_ids = $oos_query->posts;
    
            $q->set('post__not_in', $exclude_ids);
        }
    }

Out of the box "no stock" notification hook

add_filter( 'woocommerce_email_recipient_no_stock', 'change_stock_email_recipient', 10, 2 ); // For No stock notification

Would be glad if someone could point me in the right direction

1 Answers

This will hide out-of-stock products after 10 days when they went sold out.

  • After stock change events, when the product is without stock woocommerce_no_stock action hook is triggered
  • When you adjust the stock in WooCommerce backend via the product edit settings, the $meta_value is reset via woocommerce_admin_process_product_object action hook
  • Change the shop query via woocommerce_product_query action hook

Explanation via comment tags added to my answer:

// After stock change events, when the product is without stock
function action_woocommerce_no_stock( $wc_get_product ) {
    // Retrieves the date, in localized format.
    $date = wp_date( 'Y-m-d' );
    
    // Update meta
    $wc_get_product->update_meta_data( '_no_stock_date', $date );

    // Save
    $wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );

// When product is saved in WooCommerce backend
function action_woocommerce_admin_process_product_object( $product ) {
    // Get stock quantity
    $stock_quantity = $product->get_stock_quantity();
    
    // Greater than or equal to
    if ( $stock_quantity >= 1 ) {
        // Get meta value
        $no_stock_date = $product->get_meta( '_no_stock_date' );

        // NOT empty
        if ( ! empty ( $no_stock_date ) ) {
            // Update
            $product->update_meta_data( '_no_stock_date', '' );
        }
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); 

// Change the shop query
function action_woocommerce_product_query( $q, $query ) {
    // Returns true when on the product archive page (shop).
    if ( is_shop() ) {
        // Retrieves the date, in localized format.
        $date = wp_date( 'Y-m-d' );
        
        // Date - 10 days
        $date = wp_date( 'Y-m-d', strtotime( $date . ' -10 days' ) );
        
        // Get any existing meta query
        $meta_query = $q->get( 'meta_query' );
        
        // Define an additional meta query 
        $meta_query[] = array(
            'relation'    => 'OR',
            array(
                'key'     => '_no_stock_date',
                'value'   => $date,
                'compare' => '>',
                'type'    => 'date',
            ),
            array(
                'key'     => '_no_stock_date',
                'compare' => 'NOT EXISTS',
                'type'    => 'date',
            )
        );

        // Set the new merged meta query
        $q->set( 'meta_query', $meta_query );
    }
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 2 );
Related