Ecommerce Shopify WordPress Discussion

Hide WooCommerce product from specific category archive after a time period

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.
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: // Add a custom action to schedule a job when a product is published add_action( 'woocommerce_new_product', 'schedule_unassign_product_category', 10, 2 ); function schedule_unassign_product_category( $product_id, $product ) { // Schedule the job to run after 15 days wp_schedule_single_event(time() + 15 * DAY_IN_SECONDS, 'unassign_new_arrival_product_category', array($product_id)); } // Hook to run when the scheduled job is triggered add_action( 'unassign_new_arrival_product_category', 'unassign_new_arrival_product_category_callback', 10, 1 ); function unassign_new_arrival_product_category_callback( $product_id ) { $taxonomy = 'product_cat'; $terms = wp_get_post_terms($product_id, $taxonomy); // Get product categories // Loop through assigned product categories foreach( $terms as $key => $term ) { // Check if the product is in the "New Arrival" category if ( 'new-arrival' === $term->slug ) { unset($terms[$key]); // Remove 'new-arrival' category from the array if ( ! empty($terms) ) { wp_set_object_terms($product_id, $terms, $taxonomy); // Update product assigned categories } break; } } } Code goes in functions.php file of your child theme (or in a plugin). It should work.

January 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.