I have created a "New arrival" product category section in my WooCommerce website. I want product assigned to a new category will hide or will be auto removed from the "new arrival" category and remain visible in other categories if one or more categories are assigned to it.
Details & explanations:
- There is "new arrival" section in home page.
- In this product listing from the new arrival category.
- I want to hide or remove the product from the "new arrival" section after 15 days from the published date.
- But after 15 days it will remain visible in the other categories if one or more categories are assign to this product.
Example: A product "Red T shirt" is assigned "new arrival" and "sale" categories, then after 15 days, this product will be hidden or removed from the "new arrival section", but will remain visible in the "sale" category.
Here is my code so far:
// Add a custom action to schedule a job when a product is published
add_action('woocommerce_new_product', 'schedule_product_visibility_change', 10, 2);
function schedule_product_visibility_change($product_id, $product)
{
// Schedule the job to run after 15 days
wp_schedule_single_event(time() + 15 * DAY_IN_SECONDS, 'auto_hide_product_from_new_arrival', array($product_id));
}
// Hook to run when the scheduled job is triggered
add_action('auto_hide_product_from_new_arrival', 'auto_hide_product_from_new_arrival_callback');
function auto_hide_product_from_new_arrival_callback($product_id)
{
// Get product categories
$categories = wp_get_post_terms($product_id, 'product_cat', array('fields’ => ‘ids'));
// Check if the product is in the "New Arrival" category
if (in_array('new-arrival-category-id', $categories)) {
// Update product visibility
wp_set_object_terms($product_id, 'exclude-from-catalog', 'visibility', true);
}
}
// Restore product visibility if it’s removed from the “New Arrival” category before 15 days
add_action('woocommerce_remove_product_cat', 'restore_product_visibility_on_category_removal', 10, 2);
function restore_product_visibility_on_category_removal($product_id, $category)
{
// Get product categories
$categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
// Check if the product was in the "New Arrival" category
if ($category->term_id === 'new-arrival-category-id' && !in_array('new-arrival-category-id', $categories)) {
// Restore product visibility
wp_remove_object_terms($product_id, 'exclude-from-catalog', 'visibility');
}
}
But my code is incomplete and doesn’t work. Any help will be appreciated.
2
Answers
its still not working
also I have tried this code Please check and suggest what to do.
I have also tried to fetch product by the prodcut publish date and product modified date and assign to the "recent" prodcut category but the desired out put not recived .
I just want to the keep the product less then 15 days from the publish date f
You are complicating things for nothing, and there are multiple mistakes in your code… You just need to schedule "new-arrival" category unassignment after 15 days, so the product will not be visible anymore in "new-arrival" category, but will stay visible in other categories assigned to it.
Try the following:
Code goes in functions.php file of your child theme (or in a plugin). It should work.