I will try to explain what my goal is and what i already did.
I am importing a bunch of products into WooCommerce, but now i would like to add a function, that does set the product category automatically to "NEW IN" if the product is not older than 60 days. After the 60 days it should be moved out of that category again.
To be said, i am in the learning process of php with WordPress and WooCommerce, so i am sorry if i don't know certain things.
function add_product_to_new_in()
{
global $product;
// Get the date for the product published and current date
$start = date('n/h/Y', strtotime($product->get_date_created()));
$today = date('n/j/Y');
// Get the date for the start of the event and today's date
$start = new \DateTime($start);
$end = new \DateTime($today);
//FInd the difference
$difference = $start->diff($end);
$days = $difference->d;
// If the difference is less than 60, apply "NEW IN cat"
if ($days = (60 < $days)) {
wp_set_object_terms($product, 40, 'product_cat');
}
}
If you can help me in any way, i would appreciate it!