skip to Main Content

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:

  1. There is "new arrival" section in home page.
  2. In this product listing from the new arrival category.
  3. I want to hide or remove the product from the "new arrival" section after 15 days from the published date.
  4. 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


  1. Chosen as BEST ANSWER

    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

    // Add a custom column to display recent products in the category admin page
    function add_recent_products_column($columns) {
        $columns['recent_products'] = 'Recent Products';
        return $columns;
    }
    add_filter('manage_edit-product_cat_columns', 'add_recent_products_column');
    
    // Populate the custom column with recent products and assign to a specific category
    function display_recent_products_column($content, $column, $term_id) {
        if ('recent_products' === $column) {
            $category_slug = get_term($term_id, 'product_cat')->slug;
    
            // Query recent products in the specified category
            $args = array(
                'post_type'      => 'product',
                'posts_per_page' => -1,
                'tax_query'      => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'slug',
                        'terms'    => $category_slug,
                    ),
                ),
                'post_status'    => 'publish', // Consider only published products
            );
    
            $recentProducts = new WP_Query($args);
    
            // Output recent product count in the category admin page
            echo '<span>' . $recentProducts->found_posts . ' recent products</span>';
    
            // Assign recent products to a specific category
            $new_category_id = get_term_by('slug', 'recent', 'product_cat');
            if ($new_category_id) {
                foreach ($recentProducts->posts as $recentProduct) {
                    wp_set_post_terms($recentProduct->ID, $new_category_id->term_id, 'product_cat', true);
                }
            }
    
            wp_reset_postdata();
        }
    }
    add_action('manage_product_cat_custom_column', 'display_recent_products_column', 10, 3);
    
    // Schedule daily event to update recent category
    function schedule_daily_event() {
        if (!wp_next_scheduled('daily_event_hook')) {
            wp_schedule_event(time(), 'daily', 'daily_event_hook');
        }
    }
    add_action('wp', 'schedule_daily_event');
    
    // Callback function for the daily event
    function update_recent_category_daily() {
        // Query all products
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => -1,
            'post_status'    => 'publish',
        );
    
        $allProducts = new WP_Query($args);
    
        // Get the 'recent' category ID
        $recent_category_id = get_term_by('slug', 'recent', 'product_cat')->term_id;
    
        // Iterate through each product
        foreach ($allProducts->posts as $product) {
            // Get the publish date
            $post_date = get_the_date('Y-m-d', $product->ID);
    
            // Get the modified date
            $post_modified = get_the_modified_date('Y-m-d', $product->ID);
    
            // Get the date 15 days ago
            $date_15_days_ago = date('Y-m-d', strtotime('-15 days'));
    
            // Check if the product should be included or excluded from the 'recent' category
            if ($post_date >= $date_15_days_ago || $post_modified >= $date_15_days_ago) {
                // Include in the 'recent' category
                wp_set_post_terms($product->ID, $recent_category_id, 'product_cat', true);
            } else {
                // Exclude from the 'recent' category
                wp_remove_object_terms($product->ID, $recent_category_id, 'product_cat');
            }
        }
    
        wp_reset_postdata();
    }
    add_action('daily_event_hook', 'update_recent_category_daily');
    
    // Callback function for updating on publish_post hook
    function update_recent_category_on_publish($ID, $post) {
        // Check if the post is a product
        if ($post->post_type === 'product') {
            // Trigger the daily event to update the 'recent' category
            do_action('daily_event_hook');
        }
    }
    add_action('publish_post', 'update_recent_category_on_publish', 10, 2);
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search