I have an existing product category in woocommerce called 'whats-new', with an ID of 35. I want to progmatically add all (existing and new) products less than 30 days old to that category, without going through and ticking them all.
Then any products that go over the 30 days, automatically remove that category.
I feel like there must be a way to do this in WordPress, but it's proving pretty difficult for some reason and I'm going round in circles a little.
I have the following code, which is basically what I want to achieve. The time function works, but adding and remove thing category does not:
$product = wc_get_product($id);
global $product;
$datetime_created = $product->get_date_created();
$timestamp_created = $datetime_created->getTimestamp();
$datetime_now = new WC_DateTime();
$timestamp_now = $datetime_now->getTimestamp();
$time_delta = $timestamp_now - $timestamp_created;
$sixty_days = 60 * 24 * 60 * 60;
$cat_ids = array( 35 );
if ( $time_delta < $sixty_days ) {
// NEW PRODUCT - add 'whats-new' category
wp_set_object_terms( $product_id, $cat_ids, 'product_cat', true );
} elseif ( $time_delta > $sixty_days ) {
// OLD PRODUCT - remove 'whats-new' category
wp_remove_object_terms( $product_id, $cat_ids, 'product_cat', true );
}
I have an existing product category in woocommerce called 'whats-new', with an ID of 35. I want to progmatically add all (existing and new) products less than 30 days old to that category, without going through and ticking them all.
Then any products that go over the 30 days, automatically remove that category.
I feel like there must be a way to do this in WordPress, but it's proving pretty difficult for some reason and I'm going round in circles a little.
I have the following code, which is basically what I want to achieve. The time function works, but adding and remove thing category does not:
$product = wc_get_product($id);
global $product;
$datetime_created = $product->get_date_created();
$timestamp_created = $datetime_created->getTimestamp();
$datetime_now = new WC_DateTime();
$timestamp_now = $datetime_now->getTimestamp();
$time_delta = $timestamp_now - $timestamp_created;
$sixty_days = 60 * 24 * 60 * 60;
$cat_ids = array( 35 );
if ( $time_delta < $sixty_days ) {
// NEW PRODUCT - add 'whats-new' category
wp_set_object_terms( $product_id, $cat_ids, 'product_cat', true );
} elseif ( $time_delta > $sixty_days ) {
// OLD PRODUCT - remove 'whats-new' category
wp_remove_object_terms( $product_id, $cat_ids, 'product_cat', true );
}
Share
Improve this question
asked Sep 25, 2020 at 9:39
LeanneLeanne
211 silver badge4 bronze badges
1 Answer
Reset to default 1Add the codes below to a custom plugin
To add the category to new products
function set_new_product_cat( $post_id, $post, $update ) {
if("product" === $post->post_type){
// Exit out as we are updating an existing product
if ( $update ) {
return;
}
//Appends the category 35 to the product
wp_set_post_categories( $post->ID, 35, true);
}
}
add_action( 'wp_insert_post', 'set_new_product_cat', 10, 3 );
and this for setting up a cron task to once a day check for products older then 30 days that have the category still set and unset it
function remove_new_product_cat_deactivate() {
wp_clear_scheduled_hook( 'remove_new_product_cat_cron' );
}
add_action('init', function() {
add_action( 'remove_new_product_cat_cron', 'remove_new_product_cat_run_cron' );
register_deactivation_hook( __FILE__, 'remove_new_product_cat_deactivate' );
if (! wp_next_scheduled ( 'remove_new_product_cat_cron' )) {
wp_schedule_event( time(), 'daily', 'remove_new_product_cat_cron' );
}
});
function remove_new_product_cat_run_cron() {
// Get all products older then 30 days that have the category with id 35
$args = array(
'numberposts' => -1,
'category' => 35,
'post_type' => 'product',
'date_query' => array(
'before' => date('Y-m-d', strtotime('-30 days'))
)
);
$products = get_posts($args);
// Loop trough the products
foreach ( $products as $product) {
// Grab an array of currently set categories
$cats = wp_get_post_categories($product);
// Remove 35 from the list
if (($key = array_search(35, $cats)) !== false) {
unset($messages[$key]);
}
// Update the categories overwriting existing ones
wp_set_post_categories( $post->ID, $cats, false);
}
}