WooCommerce is the most popular eCommerce platform on the internet – it powers over 28% off all online stores. Today, we are going to cover how to implement WooCommerce infinite scrolling with the free Ajax Load More plugin for WordPress.

Before we begin, make sure you have both Ajax Load More and WooCommerce activated and products have been created so there is content ready for infinite scrolling 🙂

Note: The following guide and examples reference code from the official WooCommerce theme, Storefront.

**July 2019 Update**

Version 5.1 of Ajax Load More introduced built-in support for WooCommerce on pages, templates and product archives using the woocommerce shortcode parameter.

Read more about the new integration in the Building a Shortcode section below.

Implementation

There are essentially four steps to WooCommerce infinite scrolling with Ajax Load More. None of the steps are overly difficult to complete, but you will be required to make modifications to core WooCommerce templates during the process.

The implementation steps are as follows:

  1. Create Child Theme
  2. Create Repeater Template
  3. Building a Shortcode
  4. WooCommerce Template

Alright, let’s go!


Create Child Theme

Since this tutorial will require modifying core theme template files the first step will be to create a child theme – this will allow us to make modifications and not worry about conflicts when theme updates are applied.

There are many resources available online that explain exactly how to create a child theme so I won’t go into anymore detail on how to do that today.


Create Repeater Template

Next, you’ll want to create a unique WooCommerce Repeater Template for displaying your products,

Most WooCommerce compatible themes use a variation of default content-product.php template while iterating over products so you may be fine by simply copying the following template into your Ajax Load More Repeater Template.

Note: The template above was copied directly from WooCommerce core – I recommend inspecting your theme in case it’s using a modified version of this template.
/plugins/woocommerce/templates/content-product.php


Building a Shortcode

After creating the Repeater Template, you should build out a WooCommerce specific shortcode. The WooCommerce shortcode instructs Ajax Load More on what content to include while infinite scrolling the products.

Shop Home/Landing Page

For a shop homepage or generic landing page, you simply need to set woocommerce=”true” in the core Ajax Load More shortcode.

With woocommerce set as true, Ajax Load More will automatically set product as the post_type parameter and products as the css_classes parameter for the Ajax query.

Note: The majority of WordPress themes and plugins use .products as the container classname for styling WooCommerce product listings. 

Product Archives

On product archive pages, Ajax Load More will automatically detect the current term and taxonomy (e.g. product_tag or product_cat) and then behind the scenes it will set the corresponding taxonomy, taxonomy_terms, post_type and css_classes shortcode parameters.

Pro Tip: Use the css_classes shortcode parameter to add additional classes to the Ajax Load More container.

Manual Implementation

It’s not mandatory to use the built-in WooCommerce integration, you can always decide to manually build an Ajax Load More shortcode.

The code snippet above is an example of retrieving WooCommerce products on a taxonomy archive page.


WooCommerce Template

The final step is to clone and modify the archive-product.php template from /plugins/woocommerce/templates/ to the root of your current theme directory. This template used for displaying WooCommerce products so modifying this file directly will allow you to place the Ajax Load More shortcode into the correct location.

Pro Tip: Your theme may already be using custom WooCommerce templates – in that case you should modify those templates and not core WooCommerce templates as referenced below.

Once the template has been cloned, open the file in your code editor and select everything from the opening if(wc_get_loop_prop(‘total’)){ to the closing } bracket of the conditional statement. You will want to replace the selected contents, including the while() loop with the following Ajax Load More specific code.

Click here to view the complete archive-product.php template.

With the template updated and steps 1 thru 4 complete you should have a working WooCommerce infinite scroll – check out the following links to see our code implemented on a shop homepage and product archive.

Wrapping Things Up

It might seem like a lot of tasks to implement WooCommerce infinite scrolling, but keep in mind at the end of the day all you’re really doing is replacing the default WooCommerce loop with Ajax Load More.

Got any WooCommerce specific questions or suggestions? Leave them in the comments and I’ll do my best to help you out!

Comments

20 thoughts on “Infinite Scroll WooCommerce Products with Ajax Load More”

  1. Avatar

    Joao Sarmento

    October 09, 2019 @ 6:14 am
    Reply

    Hi,

    can this plugin be used to retrieve Products across a woocommerce multiste installation?

    example.com/UK
    example.com/France

    They will all have the same plugins, but Woocommerce will have different campaigns (using a crowdfunding plugin) in each sub folder.

    They will have different currencies (Pounds and Euro).

    Can the plugin be used to do a search on example.com and retrieve the campaign data from /uk and /france and show the results on example.com ?

    Please let em know.

    Cheers

    Joao

    1. Avatar

      Darren Cooney

      October 09, 2019 @ 10:36 am
      Reply

      Hi Joao,
      Unfortunately I’m not sure about this one.

  2. Avatar

    Matthew

    October 08, 2019 @ 5:03 am
    Reply

    Hi,
    I have got Ajax Load More 95% working I think. I am not sure if this is an compatibility issue with your plugin and Woo Side Cart Plugin but I cant get the “Add to Cart” Button to obtain the correct parameters so that it adds the item to the cart rather than open up the single product page.

    I have used the “create” repeater template (above) and the a href rel=”nofollow” comes back “/wp-admin/admin-ajax.php?id&post_id=9400&slug=scroll-test&canonical_url=https%3A%2F%2Fchromalytic.net.au%2Fscroll-test%2F&posts_per_page=15&page=0&offset=0&post_type=product&repeater=default&seo_start_page=1&preloaded=false&preloaded_amount=0&taxonomy=product_cat&taxonomy_terms=male-luer-integral-lock-ring&taxonomy_operator=IN&order=DESC&orderby=date&action=alm_get_posts&query_type=standard&add-to-cart=5317”
    instead of
    “/scroll-test/?add-to-cart=5317”

    I have created a scroll-test page so that you can see what happens and the differences in what I am wanting to achieve. The page has my original section using a default pagination shortcode/plugin that I want to eventually replace with your Ajax load more plugin (in the section below).

    https://chromalytic.net.au/scroll-test/

    Hopefully I have made sense – I have a feeling I just need to create an appropriate repeater template – but I am not great with php or understanding the woocommerce hooks / templates

    1. Avatar

      Darren Cooney

      October 08, 2019 @ 3:17 pm
      Reply

      Hey Matthew,
      This looks as though Woo Side Cart is not using the correct HTTP referral link to build it’s Add to Cart URLs. It’s using the URL in the Ajax reqeust.

      I think you might be best to open a ticket with them on this and let them know that Add to Cart URLs are not the correct URL when being loaded via admin-ajax.

      1. Avatar

        Matthew

        October 08, 2019 @ 9:57 pm
        Reply

        Hi Darren,

        Thanks for your reply. I will open a support ticket with them, but not sure it is related to Woo Side Cart Plugin.
        I have disable the WSC Plugin and the linking issues are still the same regarding the Product List from Top Section of the page to the bottom section under the Older Posts button (which is generated from your plugin.
        This time when I click “add to cart” button (same link a previous), it stays on the page and displays a “view cart” button underneath.
        The “add to cart” button that is under the Older Posts Section has the same link as previous and when clicked gets redirected to the Single Product Page

        I have a feeling it has something to do with the hook
        do_action( ‘woocommerce_after_shop_loop_item’ );
        which initiates/display the add to cart button. Maybe the template has been modified somehow but I would not know where/how to check. Or is there an alternative command I can try?

        1. Avatar

          Darren Cooney

          October 09, 2019 @ 12:01 pm
          Reply

          This may be an issue in your particular theme where the JS is not triggering.

          Seems to be working on my testing site.
          https://examples.connekthq.com/woocommerce/woocommerce-filtering/

  3. Avatar

    Nick

    August 19, 2019 @ 8:52 pm
    Reply

    Hi, I have implemented the Auto Scroll as directed and it worked. the only issue I have is that the format chnaged. What was 3 across in a grid is now 1 across in a list with the date added! Is there anything I need to add to the shortcode to retain the format?

    1. Avatar

      Darren Cooney

      August 20, 2019 @ 9:13 am
      Reply

      Hey Nick,
      I think you may have missed the section on creating your own the Repeater Template.
      https://connekthq.com/woocommerce-infinite-scrolling/#create-template

      Let me know if you have troubles.

  4. Avatar

    Ismail

    June 01, 2019 @ 3:29 am
    Reply

    Hi Darren,

    I have done the following steps to make it work for my WooCommerce store site with flatsome theme.
    1. copied block from content-product.php to default(repeater) template in uploads folder.
    2. cloned archive-product template from /plugins/woocommerce/templates/ to root folder of my child theme. file path in my case is /themes/flatsome-child/archive-product.php
    3. Nested following code block to if ( wc_get_loop_prop( ‘total’ ) )
    {ALM code bock(if/else) here} section in archive-product template:

    ALM Code Block Inserted:

    if(is_archive()){ // Product Archives

    $obj = get_queried_object();
    $taxonomy = $obj->taxonomy; // Get taxonomy
    $taxonomy_term = $obj->slug; // Get term
    echo do_shortcode(‘[ajax_load_more post_type="product" taxonomy="'. $taxonomy .'" taxonomy_terms="'. $taxonomy_term .'" taxonomy_operator="IN" css_classes="products" posts_per_page="6" transition="fade"]‘);

    } else { // Shop Landing Page

    echo do_shortcode(‘[ajax_load_more post_type="product" css_classes="products" posts_per_page="6" transition="fade"]‘);

    }

    Its not working. Can you please point out what i am doing wrong here.

    1. Avatar

      Ismail

      June 03, 2019 @ 2:59 am
      Reply

      Hi Darren,

      The issue is fixed by moving archive-product file from child theme root to woocommerce folder created under child theme root.
      But now the issue is that my theme layout is broken:
      1. sidebar is not showing on left side but below the products
      2. single product is showing instead of 4 in case of desktop and 2 in case of mobile
      Can you please help me fix this.
      And do you have a plan to integrate woocommerce feature in plugin options?

      Thanks.

      1. Avatar

        Darren Cooney

        June 10, 2019 @ 9:10 am
        Reply

        Hi Ismail,
        Oh, nice work. Glad to hear it’s sorted.

  5. Avatar

    John Negoita

    May 19, 2019 @ 1:56 am
    Reply

    hi Darren,

    I’m trying to implement ALM with masonry for my products in Woo.
    and I’m doing this in the archive-product.php

    echo do_shortcode(‘[ajax_load_more post_type="product" taxonomy="'. $taxonomy .'" taxonomy_terms="'. $taxonomy_term .'" taxonomy_operator="IN" css_classes="products" posts_per_page="6" transition="masonry" masonry_selector=".grid-item"]‘);

    But, I’m getting this JS error:

    Cannot read property ‘style’ of undefined at new ajaxloadmore (ajax-load-more.js:122)

    When debugging it seems that the code expects the element alm-btn-wrap to be inside the ajax-load-more-wrap element, but it’s actually outside

    What am I doing wrong?

    thanks,
    John

    1. Avatar

      Darren Cooney

      May 21, 2019 @ 8:17 am
      Reply

      Hey John,
      This error is usually related to an issue in the repeater template. If a template is closing an extra div, ALM can lose the positioning of the button.
      Can you share your template in a Gist or something?

  6. Avatar

    boombalaya

    May 03, 2019 @ 4:39 pm
    Reply

    Would it be possible to connect the shortcode with woocommerce’s attributes filter widget so users can sort by size / color?, I already tried adding the parameters in the shortcode and fetching the values from the URL via $GET[‘filter_size’] but I must be doing something wrong because no products are shown.

    1. Avatar

      boombalaya

      May 03, 2019 @ 5:18 pm
      Reply

      Nevermind, I realized product attributes are treated as taxonomies in your shortcode, all good! thanks for the great plugin.

      1. Avatar

        Darren Cooney

        May 04, 2019 @ 10:23 am
        Reply

        Yes, you need to pass the taxonomies into the shortcode. I hope to make this easier for everyone soon, but for now it’s sort of a manual process.
        Glad it’s working for you!

        1. Avatar

          boombalaya

          May 06, 2019 @ 1:37 pm
          Reply

          Thank you so much, yeah I got that working and I’m just missing one thing, I don’t know if it’s possible. Is there a way to also pass the product price range values? Thank you!

          1. Avatar

            Darren Cooney

            May 08, 2019 @ 11:49 am

            Can you show me an example?

  7. Avatar

    Jeremy

    October 03, 2018 @ 9:11 pm
    Reply

    Would it be possible for you to make a video explaining these steps as you go along? I’m not a computer science major and I’m pretty much faking it until I make it with this stuff… It’s super cool in theory, but I honestly have no idea what I’m doing and videos are much more helpful than written instructions because I can follow along as you go. Thanks!

    1. Avatar

      Darren Cooney

      October 06, 2018 @ 12:12 pm
      Reply

      Hi Jeremy,
      My plan is to start creating more video content very soon.

Leave a Reply

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