0

In my WordPress Woo-Commerce theme when a product is added to cart from shop page its added into the cart with a url e.g /shop/page/3/?add-to-cart=149 and when I switch to another page using pagination, It is unable to remove the ?add-to-cart=149 from the url and on visit to next or previous page and adds the same product again. for navigation I am using a custom function.

function custom_pagination() {
    global $wp_query;

    $pages = paginate_links( array(
            'base'         =>  esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            'prev_next' => false,
            'type'  => 'array',
            'prev_next'   => TRUE,
            'prev_text'    => __('«'),
            'next_text'    => __('»'),
        ) );
        if( is_array( $pages ) ) {
            $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
            echo '<ul class="pagination">';
            foreach ( $pages as $page ) {
                    echo "<li>$page</li>";
            }
           echo '</ul>';
        }
}

It had to handle it in the base argument but its not doing so.

-1

Paste this code into your functions.php and it will be solved.

add_action('add_to_cart_redirect', 'resolve_dupes_add_to_cart_redirect');
function resolve_dupes_add_to_cart_redirect($url = false) {
     if(!empty($url)) { return $url; } 

     return get_bloginfo('wpurl').add_query_arg(array(), remove_query_arg('add-to-cart'));
}
  • 2
    Please don't just dump code and explain why does it solve the problem. – Mark Kaplun Jan 31 '16 at 9:45
  • -1 No explanation to code, plus no acknowledgement to where this was actually copied (yes, copied) from. Plus it doesn't work for a multisite setup that uses directories. – wickywills Jun 26 '19 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.