woo commerce auto hide product (expire from listing) after specific time period

  • Hello Tech Genius,
    I need your Help .
    I am looking for a plugin or a custom php code which injects in function.php file so I can achieve below functionality in my wordpress website.

    I have created a new arrvial section my wordpress website . I want product assigned new category will hide or auto remove from the new arrival category and remain visible in other category if one or more category assigned to the same product .

    I am sharing you code snippet. Please give me the solution to achive the functionality.

    Please help me with making some changes or share your tested code.



    Code :–

    // 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');
    }

    }

    The blog I need help with is: (visible only to logged in users)

  • Hi there,

    It looks like you need help with the WooCommerce plugin but have landed on the support forum for WordPress.com sites instead.

    We don’t offer WooCommerce support on WordPress.com (unless you are upgraded to our WordPress.com Creator or Entrepreneur plan), so we are not your best point of contact. The good news is that help is available in the official WooCommerce forums: https://wordpress.org/support/plugin/woocommerce

  • The topic ‘woo commerce auto hide product (expire from listing) after specific time period’ is closed to new replies.