HomeTutorials

How to change permalink structure for Woocommerce

How to change permalink structure for Woocommerce
Like Tweet Pin it Share Share Email

You have just installed Woocommerce plugin. It is time to change the permalink structure.

In this article, you will know how to change default permalink structure. Therefore, you have will SEO friendly permalink structure.

How to set up permalink for Woocommerce

To change the permalink for Woocommerce, head over to Setting -> Permalinks.

Now you see the options which are categorized into three groups:

permalink structure Woocomerce 6

In the common settings, you choose Post name. It is the recommended setting for all WordPress websites.

Two next groups are reserved for Woocommerce.

As you know, by default, the URL of the product category is http://yourdomain.com/product-category/clothing. The ‘category-product’ word always shows up in the URL.

And the URL of the product page is http://yourdomain.com/product/happy-ninja. The ‘product’ word always shows up in the URL.

Now we change the default permalink using two nice URL patterns.

How to delete product and product-category in URL?

To delete the ‘product’ word, you need to add the code below to functions.php. Then go to Settings -> Permalinks and click Save Changes button.


//Remove product form url
function devvn_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( '/san-pham/', '/', $post_link ); //Thay product bằng slug hiện tại của bạn
   }else{
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
   }    
   return $post_link;
}
add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 );
function devvn_parse_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' ) );
   }
}
add_action( 'pre_get_posts', 'devvn_parse_request' );

To delete category-product, you install and activate WP Htaccess control plugin.

Once the plugin is activated, go to Settings -> htaccess Control. Click on Remove Taxonomies and Author Base section to view more options.

Now select Remove Categories Base. Remember to click on Save all changes to store your settings.

permalink structure Woocomerce 5

How to set up product category base which is the same as product base

Above, you know how to delete product and product-category from URL.

Now we will set up a new URL pattern for Woocommerce.

Specifically, we will have the following URL pattern:

URL of shop page: http://yourdomain.com/product

URL of product category page: http://yourdomain.com/name-of-product-category

URL of product details page: http://yourdomain.com/product/name-of-product-category/name-of-product

Here’s how to do it:

First, you need to create a page with the following slug: http://yourdomain.com/product

permalink structure Woocomerce 2

Then, go to Woocommerce -> Settings->Products->Display. Choose Product in the drop-down box of Shop page option.

permalink structure Woocomerce 3

Click save changes.

Next, go to Settings->Permalinks. At Optional section, enter product at Product category base option.

Scroll down to Product permalinks section. Enter /product/%product_cat%/ at the custom base.

Click Save Changes button.

permalink structure Woocomerce 4

Finally, add the following code to functions.php. Then back to Settings -> Permalinks and click Save changes button to update new permalink structure.


add_filter( 'rewrite_rules_array', function( $rules )
{
$new_rules = array(
'products/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
'products/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',
);
return $new_rules + $rules;
} );

That’s it.

You already know to have a nice permalink structure for Woocommerce.

If you have any trouble setting up permalink for Woocommerce, leave your comment below.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
Scroll Up