0

I'm using WooCommerce. And WC has an ugly link structure /product/category-name/product-name. I removed /product/ from the url. But now I'm facing an issue. On the product page reviews are shown, when they have a specific amount they will be showen with a pagination.

Right now the url of those links in the pagination is: /category/product-name/comment-page-2/#comments

That is quite good, although I'd prefer /category/product-name/comment/2 or /category/product-name/page/2. BUT the real problem here is, they don't show up. They automatically redirect the pages higher dan page 1 to the main productpage. And my custom permalink structure (without /product/) is the issue!

When I visit /product/category/product-name/comment-page-2/#comments the right comments are shown.

I use this code to remove /product/ from the link structure:

* Remove /product/ or /shop/ ... support %product_cat%
*/
function SNL_remove_slug( $post_link, $post ) {
    if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
        return $post_link;
    }
    if ('product' == $post->post_type) {
        $post_link = str_replace( '/product/', '/', $post_link ); //replace "product" to your slug
    } else {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'SNL_remove_slug', 10, 2 );

function SNL_woo_product_rewrite_rules($flash = false) {
    global $wp_post_types, $wpdb;

    $siteLink = esc_url(home_url('/'));

    foreach ($wp_post_types as $type=>$custom_post) {
        if ($type == 'product') {
            if ($custom_post->_builtin == false) {
                $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
                            FROM {$wpdb->posts} 
                            WHERE {$wpdb->posts}.post_status = 'publish'
                            AND {$wpdb->posts}.post_type = '{$type}'";
                $posts = $wpdb->get_results($querystr, OBJECT);
                foreach ($posts as $post) {
                    $current_slug = get_permalink($post->ID);
                    $base_product = str_replace($siteLink,'',$current_slug);
                    add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');
                }
            }
        }
    }

    if ($flash == true) {
        flush_rewrite_rules(false);
    }
}
add_action('init', 'SNL_woo_product_rewrite_rules');

function SNL_woo_new_product_post_save($post_id){
    global $wp_post_types;
    $post_type = get_post_type($post_id);
    foreach ($wp_post_types as $type=>$custom_post) {
        if ($custom_post->_builtin == false && $type == $post_type) {
            SNL_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'SNL_woo_new_product_post_save');

Does anyone of you guys know how I fix this pagination custom permalink problem?

  • Did you tried changing the permalink from WP Admin > Settings -> Permalinks page ? There is a section named "Product permalinks". – Shazzad Apr 13 '20 at 18:08
  • Yep, but /product/ is required. So I’ve to do that programmatically. – T94 Apr 14 '20 at 19:33

Your Answer

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

Browse other questions tagged or ask your own question.