Hide recent ‘product list’ when empty on Home Page

This topic has 4 replies, 3 voices, and was last updated 1 month, 1 week ago ago by Tony Rodriguez

  • Avatar: islam
    islam
    Participant
    January 24, 2026 at 10:45

    Hi I’m displaying a list of recently viewed products on Home page, but i need to hide the section (including the title) when the list the empty.

    Files is visible for topic creator and
    support staff only.
    3 Answers
    Avatar: Samir Malik
    Samir Malik
    Support staff
    January 24, 2026 at 11:38

    Hello,

    Unfortunately, there is no available setting to hide the entire section when there are no products.

    To achieve this, you would need to use custom JavaScript code. Please add the following code to the **functions.php** file located in your child theme:

    
    <?php
    // Hide "Previously Viewed" section when products are empty
    add_action('wp_footer', function() {
        ?>
        <script>
        (function() {
            // Target the section by its data-id
            const section = document.querySelector('[data-id="b4f1946"]');
            
            if (!section) return;
            
            // Function to check if section has products
            function checkProducts() {
                // Look for product elements within the section or after it
                const productContainer = section.nextElementSibling;
                
                // Common selectors for WooCommerce/Elementor products
                const products = section.querySelectorAll('.product, .woocommerce-product, .elementor-widget-products, .products .product');
                
                // Alternative: check the next sibling section for products
                const nextSectionProducts = productContainer?.querySelectorAll('.product, .woocommerce-product, .elementor-widget-products, .products .product');
                
                // Hide section if no products found
                if (products.length === 0 && (!nextSectionProducts || nextSectionProducts.length === 0)) {
                    section.style.display = 'none';
                } else {
                    section.style.display = '';
                }
            }
            
            // Check on load
            checkProducts();
            
            // Re-check after a short delay (in case products load dynamically)
            setTimeout(checkProducts, 500);
            setTimeout(checkProducts, 1000);
            
            // Optional: Use MutationObserver to detect dynamic changes
            const observer = new MutationObserver(checkProducts);
            observer.observe(document.body, { childList: true, subtree: true });
        })();
        </script>
        <?php
    });
    ?>
    

    Please note that custom code implementation is beyond the scope of our standard support services. You will need to maintain and adjust it on your own.

    Thank you for your understanding.

    Best regards,
    8Theme Team

    Avatar: islam
    islam
    Participant
    March 26, 2026 at 07:46

    Thanks for the support! My topic “Hide recent ‘product list’ when empty on Home Page” has been successfully resolved.

    Avatar: Tony Rodriguez
    Tony Rodriguez
    Support staff
    March 26, 2026 at 07:46

    Dear islam,

    It’s great having you in our WordPress & WooCommerce community!

    Every insight you share helps us refine XStore and build tools that empower thousands of online store owners worldwide.

    Together, we grow stronger with every release.

    Topic closed.
    The 8Theme Team

  • Viewing 4 results - 1 through 4 (of 4 total)

The issue related to '‘Hide recent ‘product list’ when empty on Home Page’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.