Trying to insert a div ID link into Woocommerce shortcode Pagination

I’m using the following Woocommerce shortcode half way down a landing page:

[products author="4" columns="4"  orderby="rand" order="DESC" visibility="visible" paginate="true" limit="24" class="allproducts"]

When clicking on the pagination buttons it loads the next page at the top of the landing page.

Read More

I have a div with the ID “#products” above the shortcode. I am trying to insert the ID into the pagination so when the next page loads, it will jump to the ID at the top of the products.

After Googling for hours I found this code and put it in functions.php but, while it jumps to the ID, it does not load the next page:

add_filter( 'woocommerce_pagination_args',  'd3c_pagination' );
   function d3c_pagination( $args ) {
      $args['add_fragment'] = '#products';
   return $args;
   }

I keep searching but I can’t find what I’m doing wrong or how to accomplish what I need.

Any help would be beyond appreciated.

Related posts

Leave a Reply

1 comment

  1. A certain chatty AI found me a solution.

    add_action( 'wp_footer', 'custom_pagination_script' );
      function custom_pagination_script() {
      ?>
        <script type="text/javascript">
        jQuery( document ).ready( function( $ ) {
            $( '.page-numbers a' ).click( function( event ) {
                event.preventDefault();
                var link = $( this ).attr( 'href' );
                window.location = link + '#d3c-psf-products';
            } );
        } );
        </script>
        <?php
    }
    
    add_action( 'wp_footer', 'custom_sorting_script' );
    function custom_sorting_script() {
        ?>
        <script type="text/javascript">
        jQuery( document ).ready( function( $ ) {
            $( '.woocommerce-ordering' ).submit( function( event ) {
                event.preventDefault();
                var link = $( this ).attr( 'action' ) + '?' + $( this ).serialize() + '#d3c-psf-products';
                window.location = link;
            } );
        } );
        </script>
        <?php
    }
    

    This solution does what I want perfectly.