'How do I automatically set WooCommerce Product to draft after 14 days of creation
Hi I want to change published products to drafts automatically after 14 days of its creation only simple and variable product type to go to drafts after 14 days can anyone help please
Solution 1:[1]
<?php
add_action('save_post_product', 'mp_sync_on_product_save', 10, 3);
function mp_sync_on_product_save( $post_id, $post, $update ) {
if ( $update ) {
return;
}
$_14_days_later = strtotime( '+14 day' );
wp_schedule_single_event( $_14_days_later, 'wp_cron_set_product_status_to_draft', array( $post_id ) );
}
add_action( 'wp_cron_set_product_status_to_draft', 'set_product_status_to_draft', 10, 1 );
function set_product_status_to_draft( $product_id = null ) {
$product_id = intval( $product_id );
if ( $product_id <= 0 ) {
return;
}
$product = wc_get_product( $product_id );
if ( $product === null || $product === false ) {
return;
}
if ( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
wp_update_post( array(
'ID' => $product_id,
'post_status' => 'draft'
) );
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Artemy Kaydash |
