I am using this code from here.
It deletes products if both the status is “Out of stock” and the product itself was edited more than 60 days ago.
-
How can I change this code so that it counts 60 days from the moment the “out of stock” status is established for all its products (including all variations?)
Now he is counting from the date of the last edit of the product – and this is not entirely correct. -
Also, after, thanks to this code, the product is moved to the cart and then deleted from there automatically from the woocommerce settings, for some reason it is still displayed in the catalog.
Specifically: There was 1 product in the “Cactus” subcategory. It had not been edited for 60 days and had the status “out of stock” – it was deleted. But if you go to in administrator settings“Products” - “Categories” - “Plants” - “Cactus”
– in the counter for the number of products in the category, I see one, as if this product had not been deleted.
Any help please?
add_action('init', 'automatically_trash_sold_items_in_woocommerce_callback');
function automatically_trash_sold_items_in_woocommerce_callback() {
if ( false === ( $automatically_trash_sold_items_in_woocommerce = get_transient( 'automatically_trash_sold_items_in_woocommerce' ) ) ) {
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'
AND P.post_modified < DATE_SUB( NOW(), INTERVAL 60 DAY )");
set_transient( 'automatically_trash_sold_items_in_woocommerce', true, 12 * HOUR_IN_SECONDS );
}
}