11

I'm using WooCommerce on a WordPress and it adds product & product-category to the

URLs.

http://dev.unwaveringmedia.com/8dim/product-category/all-party-supplies/ http://dev.unwaveringmedia.com/8dim/product/14-snowman-serving-tray/

I need to remove 'product' & 'product-category' from the URLs. Is there any way to modify the permalinks and remove them?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Bhagya Shree
  • 111
  • 1
  • 1
  • 4
  • Have you checked settings ->permalinks? – Yahya Hussein Apr 17 '17 at 07:35
  • @Bhagya Shree it's not recommended, read this https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/ – kebir Apr 17 '17 at 08:03
  • you should use of woocommerce seo perfect plugin because it's good for seo and create best url for you I use most way to remove category but I saw there are dot in canonical so i decided to use this plugin cheers – amh Sep 06 '20 at 14:05

6 Answers6

24

Yes. But Please read this article first https://docs.woocommerce.com/document/removing-product-product-category-or-shop-from-the-urls/

You can change this by:

  1. you can change the permalinks in Settings > permalink > optional > Product category base= ./ (type ./ in Product category base).

  2. Be sure that you don’t have any page, post or attachment with the same name (slug) as the category page or they will collide and the code won’t work.

  3. Install and activate the plugin below: (For more info please see https://timersys.com/remove-product-category-slug-woocommerce/)

`

<?php
/*
Plugin Name: Remove product-category slug
Plugin URI: https://timersys.com/
Description: Check if url slug matches a woocommerce product category and use it instead
Version: 0.1
Author: Timersys
License: GPLv2 or later
*/
add_filter('request', function( $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;
});`

enter image description here

For more info please see https://timersys.com/remove-product-category-slug-woocommerce/

Shehroz Altaf
  • 618
  • 1
  • 10
  • 17
7

Noone mentioned how to remove /product/ so here it goes:

function na_remove_slug( $post_link, $post, $leavename ) {
    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    $post_link = str_replace( '/product/', '/', $post_link );
    return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

function change_slug_struct( $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' ) );
    } elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) {
        $query->set( 'post_type', array( 'post', 'product', 'page' ) );

        // We also need to set the name query var since redirect_guess_404_permalink() relies on it.
        $query->set( 'name', $query->query['pagename'] );
    }
}
add_action( 'pre_get_posts', 'change_slug_struct', 99 );

I've added priority since some themes/plugins can conflict with this rule, which happened to me.

Goran Jakovljevic
  • 2,714
  • 1
  • 31
  • 27
  • This is a simple solution but it doesn't work if there is %product_cat% in the product base and there are multiple categories in the URL, for example: /parent-cat/child-cat/product-slug/ – Hasan Akhtar Oct 23 '21 at 07:30
  • This solution works nicely for me. It's simple and efficient. – Anh Tran Jul 31 '23 at 02:48
  • Works pasting it straight into `functions.php`, keeps the `/product/` permalink structure working too if you need it. – YourManDan Aug 11 '23 at 16:32
3

It wasn't working with pagination for me. Here is full code that's working.

add_filter('request', function( $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;
});

This will add new rewrite rule that will paginate query.

add_action('init', 'do_rewrite');
function do_rewrite(){
add_rewrite_rule( '[^/]+/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&paged=$matches[2]', 'top' );
add_filter( 'query_vars', function( $vars ){
    $vars[] = 'attachment';
    $vars[] = 'paged';
    return $vars;
} );}
Alex
  • 31
  • 1
1

Using Shehroz Altaf's trick does the job just (almost) perfectly.

Adding the following between the $slug and $exists declarations will make it work with sub-categories too.

$slug_array = explode( '/', $slug );
$slug = array_values(array_slice($slug_array, -1))[0];
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
1

You can use the following filter: woocommerce_register_post_type_product

Here is an example if you only want to remove /product/ but keep the category.

add_filter( 'woocommerce_register_post_type_product', function($var) {
    $var['rewrite'] = str_replace('/product/', '/', $var['rewrite']);
    return $var;
});

Then make sure to flush the permalinks by updating the permalinks in the Admin panel.

Keep in mind you will still see '/product/' in the permalink structure in the Admin Panel Permalinks, but it will not have an affect.

ggedde
  • 582
  • 5
  • 12
-1

Other solutions all failed for me, after some trial and error I came up with this solution...

  1. Install free 'WOO CATEGORY BASE PERMALINK FIXER' plugin from https://masterns-studio.com/code-factory/wordpress-plugin/woo-category-base/
  2. Set Product Category Base Permalinks to : shop
  3. Set Custom Base Permalinks to : /shop/%product_cat%/

URLs will then look like

http://www.example.com/shop/category/sub-category/product

Seems to work fine for pagination and sub categories

fightstarr20
  • 11,682
  • 40
  • 154
  • 278