Support » Plugin: WooCommerce » 404 error in pagination in categories of woocommerce with common url base /shop/

  • Resolved wladm

    (@wladm)


    I can’t find a solution — unification of url bases of products and categories in woocommerce.

    I have found only one working solution:

    In Settings -> Permalinks:
    Category base change on shop
    Custom Structure change on /shop/%product_cat%/

    and add to functions.php code:

    add_filter( 'request', 'change_requerst_vars_for_product_cat' );
    add_filter( 'term_link', 'term_link_filter', 10, 3 );
    add_filter( 'post_type_link', 'wpp_remove_slug', 10, 3 );
    add_action( 'pre_get_posts', 'wpp_change_request' );
    
    function change_requerst_vars_for_product_cat($vars) {
    
        global $wpdb;
        if ( ! empty( $vars[ 'pagename' ] ) || ! empty( $vars[ 'category_name' ] ) || ! empty( $vars[ 'name' ] ) || ! empty( $vars[ 'attachment' ] ) ) {
          $slug   = ! empty( $vars[ 'pagename' ] ) ? $vars[ 'pagename' ] : ( ! empty( $vars[ 'name' ] ) ? $vars[ 'name' ] : ( ! empty( $vars[ 'category_name' ] ) ? $vars[ 'category_name' ] : $vars[ 'attachment' ] ) );
          $exists = $wpdb->get_var( $wpdb->prepare( "SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = 'product_cat' AND t.slug = %s", array( $slug ) ) );
          if ( $exists ) {
            $old_vars = $vars;
            $vars     = array( 'product_cat' => $slug );
            if ( ! empty( $old_vars[ 'paged' ] ) || ! empty( $old_vars[ 'page' ] ) ) {
              $vars[ 'paged' ] = ! empty( $old_vars[ 'paged' ] ) ? $old_vars[ 'paged' ] : $old_vars[ 'page' ];
            }
            if ( ! empty( $old_vars[ 'orderby' ] ) ) {
              $vars[ 'orderby' ] = $old_vars[ 'orderby' ];
            }
            if ( ! empty( $old_vars[ 'order' ] ) ) {
              $vars[ 'order' ] = $old_vars[ 'order' ];
            }
          }
        }
    
        return $vars;
    
      }
    
    function term_link_filter( $url, $term, $taxonomy ) {
    
        $url = str_replace( "/product-category/", "/shop/", $url );
        return $url;
    
      }
    
    function wpp_remove_slug( $post_link, $post, $name ) {
    
        if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
          return $post_link;
        }
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    
        return $post_link;
    
      }
    
    function wpp_change_request( $query ) {
    
        if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query[ 'page' ] ) ) {
          return;
        }
        if ( ! empty( $query->query[ 'name' ] ) ) {
          $query->set( 'post_type', array( 'post', 'product', 'page' ) );
        }
    
    }

    After that all work good, but pagination doesn’t work.

    It works only on pages of subcategories https://dverinevada.ru/shop/vhodnye-dveri/kondor/page/2/
    But on pages of categories there is 404 error https://dverinevada.ru/shop/vhodnye-dveri/page/2/

    We can add such code in functions.php to fix 404 error:

    function codernote_request($query_string ) {
    if ( isset( $query_string['page'] ) ) {
    if ( ''!=$query_string['page'] ) {
    if ( isset( $query_string['name'] ) ) {
    unset( $query_string['name'] ); }
    }
    }
    return $query_string;
    }
    add_filter('request', 'codernote_request');

    After that pagination pages doesn’t have 404 error, but there are no products (instead of them message «No products matching your request were found.»).

    I ask for help in solving this problem.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Phillip C.

    (@phillipwoo)

    Hi @wladm,

    It has been a couple of weeks since you submitted this so I’m curious if you’ve resolved this issue yet? If not, can you try resaving your permalinks again? When dealing with URL stuff it can be a weird but helpful way to resolve things like this. It may not work but it’s a simple change that has worked for me many times in cases similar to this.

    Please let me know how it goes and well go from there if it doesn’t fix this. Also, if you could enable logging for as much as you can in your site we may be able to find out what’s going on from those as well.

    Thanks!

    Plugin Support Shaun Kuschel

    (@shaunkuschel)

    Automattic Happiness Engineer

    Hey @wladm,

    Since we haven’t heard anything in a while, I’m going to mark this thread as resolved. However, if you are still having issues, please try the steps Phillip mentioned above and let us know how it goes. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘404 error in pagination in categories of woocommerce with common url base /shop/’ is closed to new replies.