< infinite scroll products

// infinite scroll products after finish categorys sohow other rentom categorys products 

// [custom_infinite_scroll_products]
// [custom_infinite_scroll_products category="electronics"]
add_action('wp_enqueue_scripts', 'custom_infinite_scroll_scripts_with_inline_js');
function custom_infinite_scroll_scripts_with_inline_js() {
    wp_enqueue_script('jquery'); // Ensure jQuery is loaded

    // Inline JavaScript for infinite scroll functionality
    $inline_js = <<<EOD
    jQuery(function($){
        var page = 2;
        var loading = false;
        var allProductsLoaded = false; // New variable to track if all products have been loaded

        $(window).scroll(function() {
            if (!loading && !allProductsLoaded && $(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                $('#infinite_scroll_loader').show();
                loading = true;

                var category = $('#infinite_scroll_category').val(); // Get the selected category

                $.ajax({
                    url: ajax_params.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'load_more_products',
                        page: page,
                        category: category, // Pass the selected category to the AJAX handler
                    },
                    success: function(response) {
                        if (response) {
                            $('.products').append(response); // Append new products
                            page++;
                            $('#infinite_scroll_loader').hide();
                            loading = false;
                        } else {
                            $('#infinite_scroll_loader').html('No more products to display.');
                            allProductsLoaded = true; // Mark all products as loaded
                        }
                    }
                });
            }
        });
    });
EOD;
    // Localize the script with new data
    $translation_array = array('ajax_url' => admin_url('admin-ajax.php'));
    wp_localize_script('jquery', 'ajax_params', $translation_array);

    // Add the inline script
    wp_add_inline_script('jquery', $inline_js);
}

// Shortcode to display products with infinite scroll
add_shortcode('custom_infinite_scroll_products', 'custom_infinite_scroll_products_function');
function custom_infinite_scroll_products_function($atts) {
    // Extract category attribute from shortcode
    $attributes = shortcode_atts(array(
        'category' => '', // Default category is blank, showing all
    ), $atts);

    $category = $attributes['category'];

    ob_start(); // Start output buffering

    // Add a hidden input field to store the selected category
    echo '<input type="hidden" id="infinite_scroll_category" value="' . esc_attr($category) . '">';

    // Use WooCommerce shortcode for products, adjust limit and columns as needed
    echo do_shortcode('[products limit="10" columns="4" category="' . esc_attr($category) . '"]');

    // Loader for visual feedback
    echo '<div id="infinite_scroll_loader" style="display:none;">Loading...</div>';

    return ob_get_clean(); // Return the buffered output
}

// AJAX action to load more products
add_action('wp_ajax_load_more_products', 'load_more_products');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products');
function load_more_products() {
    $page = $_POST['page'];
    $category = isset($_POST['category']) ? $_POST['category'] : ''; // Get category from AJAX request

    // Check if the current category has more products
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'paged' => $page,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $category,
            ),
        ),
    );

    $loop = new WP_Query($args);

    if ($loop->have_posts()) {
        while ($loop->have_posts()): $loop->the_post();
            wc_get_template_part('content', 'product');
        endwhile;
    } else {
        // If no more products in the current category, fetch random products
        $args = array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'posts_per_page' => 10,
            'orderby' => 'rand', // Get random products
        );

        $random_loop = new WP_Query($args);

        if ($random_loop->have_posts()) {
            while ($random_loop->have_posts()): $random_loop->the_post();
                wc_get_template_part('content', 'product');
            endwhile;
        } else {
            echo ''; // No more posts
        }
    }
    wp_reset_postdata();
    die();
}
//infinite scroll products but prodoct finish no More 
// [custom_infinite_scroll_products]
// [custom_infinite_scroll_products category="electronics"]
// Enqueue scripts and define infinite scroll functionality
add_action('wp_enqueue_scripts', 'custom_infinite_scroll_scripts_with_inline_js');
function custom_infinite_scroll_scripts_with_inline_js() {
    wp_enqueue_script('jquery'); // Ensure jQuery is loaded

    // Inline JavaScript for infinite scroll functionality
    $inline_js = <<<EOD
    jQuery(function($){
        var page = 2;
        var loading = false;
        var allProductsLoaded = false; // New variable to track if all products have been loaded

        $(window).scroll(function() {
            if (!loading && !allProductsLoaded && $(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                $('#infinite_scroll_loader').show();
                loading = true;

                var category = $('#infinite_scroll_category').val(); // Get the selected category

                $.ajax({
                    url: ajax_params.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'load_more_products',
                        page: page,
                        category: category, // Pass the selected category to the AJAX handler
                    },
                    success: function(response) {
                        if (response) {
                            $('.products').append(response); // Append new products
                            page++;
                            $('#infinite_scroll_loader').hide();
                            loading = false;
                        } else {
                            $('#infinite_scroll_loader').html('No more products to display.');
                            allProductsLoaded = true; // Mark all products as loaded
                        }
                    }
                });
            }
        });
    });
EOD;
    // Localize the script with new data
    $translation_array = array('ajax_url' => admin_url('admin-ajax.php'));
    wp_localize_script('jquery', 'ajax_params', $translation_array);

    // Add the inline script
    wp_add_inline_script('jquery', $inline_js);
}

// Shortcode to display products with infinite scroll
add_shortcode('custom_infinite_scroll_products', 'custom_infinite_scroll_products_function');
function custom_infinite_scroll_products_function($atts) {
    // Extract category attribute from shortcode
    $attributes = shortcode_atts(array(
        'category' => '', // Default category is blank, showing all
    ), $atts);

    $category = $attributes['category'];

    ob_start(); // Start output buffering

    // Add a hidden input field to store the selected category
    echo '<input type="hidden" id="infinite_scroll_category" value="' . esc_attr($category) . '">';

    // Use WooCommerce shortcode for products, adjust limit and columns as needed
    echo do_shortcode('[products limit="10" columns="4" category="' . esc_attr($category) . '"]');

    // Loader for visual feedback
    echo '<div id="infinite_scroll_loader" style="display:none;">Loading...</div>';

    return ob_get_clean(); // Return the buffered output
}

// AJAX action to load more products
add_action('wp_ajax_load_more_products', 'load_more_products');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products');
function load_more_products() {
    $page = $_POST['page'];
    $category = isset($_POST['category']) ? $_POST['category'] : ''; // Get category from AJAX request

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'paged' => $page,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $category,
            ),
        ),
    );

    $loop = new WP_Query($args);
    if ($loop->have_posts()) {
        while ($loop->have_posts()): $loop->the_post();
            wc_get_template_part('content', 'product');
        endwhile;
    } else {
        echo ''; // No more posts
    }
    wp_reset_postdata();
    die();
}


// infinite scroll products agin and agin same prodoct display  
// [custom_infinite_scroll_products]
// [custom_infinite_scroll_products category="electronics"]

add_action('wp_enqueue_scripts', 'custom_infinite_scroll_scripts_with_inline_js');
function custom_infinite_scroll_scripts_with_inline_js() {
    wp_enqueue_script('jquery'); // Ensure jQuery is loaded

    // Inline JavaScript for infinite scroll functionality
    $inline_js = <<<EOD
    jQuery(function($){
        var page = 2;
        var loading = false;

        $(window).scroll(function() {
            if (!loading && $(window).scrollTop() + $(window).height() > $(document).height() - 100) {
                $('#infinite_scroll_loader').show();
                loading = true;

                var category = $('#infinite_scroll_category').val(); // Get the selected category

                $.ajax({
                    url: ajax_params.ajax_url,
                    type: 'POST',
                    data: {
                        action: 'load_more_products',
                        page: page,
                        category: category, // Pass the selected category to the AJAX handler
                    },
                    success: function(response) {
                        if (response) {
                            $('.products').append(response); // Append new products
                            page++;
                            $('#infinite_scroll_loader').hide();
                            loading = false;
                        } else {
                            $('#infinite_scroll_loader').html('No more products to display.');
                        }
                    }
                });
            }
        });
    });
EOD;
    // Localize the script with new data
    $translation_array = array('ajax_url' => admin_url('admin-ajax.php'));
    wp_localize_script('jquery', 'ajax_params', $translation_array);

    // Add the inline script
    wp_add_inline_script('jquery', $inline_js);
}

// Shortcode to display products with infinite scroll
add_shortcode('custom_infinite_scroll_products', 'custom_infinite_scroll_products_function');
function custom_infinite_scroll_products_function($atts) {
    // Extract category attribute from shortcode
    $attributes = shortcode_atts(array(
        'category' => '', // Default category is blank, showing all
    ), $atts);

    $category = $attributes['category'];

    ob_start(); // Start output buffering

    // Add a hidden input field to store the selected category
    echo '<input type="hidden" id="infinite_scroll_category" value="' . esc_attr($category) . '">';

    // Use WooCommerce shortcode for products, adjust limit and columns as needed
    echo do_shortcode('[products limit="10" columns="4" category="' . esc_attr($category) . '"]');

    // Loader for visual feedback
    echo '<div id="infinite_scroll_loader" style="display:none;">Loading...</div>';

    return ob_get_clean(); // Return the buffered output
}

// AJAX action to load more products
add_action('wp_ajax_load_more_products', 'load_more_products');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products');
function load_more_products() {
    $page = $_POST['page'];
    $category = isset($_POST['category']) ? $_POST['category'] : ''; // Get category from AJAX request

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'paged' => $page,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                'terms'    => $category,
            ),
        ),
    );

    $loop = new WP_Query($args);

    if ($loop->have_posts()) {
        while ($loop->have_posts()): $loop->the_post();
            wc_get_template_part('content', 'product');
        endwhile;
    } else {
        // Reset the query and display all products again in random order
        $args['paged'] = -1; // Set the 'paged' parameter to -1 to retrieve all posts at once
        $args['orderby'] = 'rand'; // Add 'orderby' parameter to shuffle the order
        $loop = new WP_Query($args);

        if ($loop->have_posts()) {
            while ($loop->have_posts()): $loop->the_post();
                wc_get_template_part('content', 'product');
            endwhile;
        } else {
            echo 0; // No more posts
        }
    }

    wp_reset_postdata();
    die();
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
🏠 Home 🛍️ Category 🧑🏻‍⚕️ Account 💝 0 Wishlist 🏦 Wallet
Custom Icon
0
0.00
Scroll to Top