0

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.

0

I had the same problem but on a custom archive-product.php page which contains a custom query to get my products with only 10 results per page.

I had 404 pages on shop page and categories page with the second page of pagination.

After analysis, I found that the main query of WooCommerce asks for 16 results but I had only 12 products in my database. So the main query returns nothing.

To change that, I need to do in admin : Appearance > Customize > WooCommerce > Product Catalog and change the value to 1 for Products per row and Rows per page.

With that the main query returns always 1 result and I had no more 404 pages.

I hope that will help you or another.

Source : https://docs.woocommerce.com/document/change-number-of-products-displayed-per-page/

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.