1

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

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Nik7
  • 346
  • 2
  • 16

1 Answers1

1

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 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • This is very strange. I use WP staging plugin to clone the prod system and test all the stuff. When I test this code, after apply the code from "// Change the shop query" the staging system goes down AND also the prod system goes down.. How can that be possible? I Do not test this on Prod.. I do it on a staging system but Prod system goes also down. I do not see any log entry related to this very strange. I insert above the logs maybe someone is able to see something wrong. – Nik7 Jun 17 '21 at 10:51
  • 1
    @Nik7 Please solve these problems first, only then can it be determined if there are new problems with my answer. This doesn't seem to be the case for me. These issues also seem to be unrelated to this answer and your original question. I therefore believe that it is better to open a new question, if you can't solve the issues, instead of editing your existing question. – 7uc1f3r Jun 24 '21 at 09:44