Lập TrìnhWordpress

[Woocommerce] Làm Thế Nào Để Xóa Đường Dẫn Chuyên Mục Trong Woocommerce

Khám phá cách loại bỏ slug danh mục sản phẩm trong WooCommerce để tối ưu hóa cấu trúc URL và nâng cao trải nghiệm SEO cho website của bạn. Bài hướng dẫn này sẽ cung cấp các bước chi tiết để tinh chỉnh đường dẫn sản phẩm, giúp chúng trở nên gọn gàng, dễ đọc và thân thiện với cả người dùng lẫn công cụ tìm kiếm. Tìm hiểu những mẹo thực tế để xây dựng một cửa hàng trực tuyến chuyên nghiệp, với cấu trúc URL rõ ràng và súc tích.

Cấu Trúc URL Mặc Định Của Woocommerce Là Gì ?

Khi bạn thiết lập cửa hàng của mình bằng WooCommerce trên WordPress CMS, cấu trúc URL của trang web, còn được gọi là liên kết cố định, đóng vai trò quan trọng trong cách xác định và truy cập các trang sản phẩm của bạn. Theo mặc định, liên kết cố định WooCommerce bao gồm các phân loại và cấu trúc cố định như sau

Cấu trúc url mặc định của bạn thường trông như thế này:

Main Shop Page

http://hnitsoft.com/shop/

Category Page

http://hnitsoft.com/product-category/category-slug/

Product Page

http://hnitsoft.com/product/product-slug/

Trên đây là cấu trúc URL mặc định của Woocommerce để phân loại danh mục sản phẩm và từng sản phẩm của cửa hàng bạn.

Cách xóa slug danh mục sản phẩm trong WooCommerce

Cách 1: Phương pháp thủ công để xóa slug danh mục sản phẩm

Đi đến Settings > Permalinks trong menu Wordpress.

Kéo xuống dưới Tùy Chọn (Optional)

Trong Danh mục sản phẩm (Product category base), nhập dấu “.”

Sau đó nhấn nút “Lưu Lại” để thay đổi

Đảm bảo không có bài đăng, trang, danh mục nào trùng tên với danh mục sản phẩm để tranh xung đột cấu trúc url

Xóa Slug Danh Mục Sản Phẩm dành cho các sản phẩm đã có

Nếu bạn muốn xóa danh mục sản phẩm với các sản phẩm đã tạo. Thì làm theo mình

Đến menu Sản phẩm > Tất cả sản phẩm (Product > All Products)

Chọn sản phẩm muốn sửa và nhấp vào Sửa nhanh (Quick Edit)

Nhấp vào trường Đường dẫn (Slug) bên dưới Tiêu đề (Title)

Chỉnh sửa lại url xóa cấu trúc url danh mục sản phẩm. Sau đó bấm lưu lại là xong

Bạn có thể cần thiết lập chuyển hướng 301 để xử lý các URL trước đó và đảm bảo khách truy cập được hướng dẫn đến đúng trang sản phẩm mà không có lỗi.

Cách 2: Sử dụng plugin

Ở các này bạn có thể sử dụng các plugin SEO như Rank Math SEO, Yoat Seo để xóa đi category

Cách 3: Xóa đường dẫn danh mục sản phẩm (product-category) và sản phẩm (products) bằng code

Hướng dẫn xóa bỏ chữ product trong đường dẫn

Trước tiên các bạn dán đoạn code sau vào file functions.php của theme đang sử dụng. Sau đó hãy update lại permalink (Setting -> Permalinks -> Save changes) và thưởng thức thành quả.

<?php
/*
* Code Bỏ /product/ hoặc /cua-hang/ hoặc /shop/ ... có hỗ trợ dạng %product_cat%
* Thay /cua-hang/ bằng slug hiện tại của bạn
*/
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( '/cua-hang/', '/', $post_link ); //Thay cua-hang 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 );
/*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/
function devvn_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');                    
                    add_rewrite_rule($base_product.'comment-page-([0-9]{1,})/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&cpage=$matches[1]', 'top');
                    add_rewrite_rule($base_product.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&feed=$matches[1]','top');
                }
            }
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_woo_product_rewrite_rules');
/*Fix lỗi khi tạo sản phẩm mới bị 404*/
function devvn_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) {
            devvn_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'devvn_woo_new_product_post_save');

Hướng dẫn loại bỏ chữ product-category trong đường dẫn KHÔNG cần plugin

Bạn chỉ cần copy đoạn code sau vào file functions.php của theme đang sử dụng sau đó vào update lại permalink (Setting -> Permalink -> Save change) là có thể xóa bỏ dc chữ product-category ra khỏi đường dẫn rồi.

<?php
/*
* Remove product-category in URL
* Thay product-category bằng slug hiện tại của bạn. Mặc định là product-category
*/
add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 );
function devvn_product_cat_permalink( $url, $term, $taxonomy ){
    switch ($taxonomy):
        case 'product_cat':
            $taxonomy_slug = 'product-category'; //Thay bằng slug hiện tại của bạn. Mặc định là product-category
            if(strpos($url, $taxonomy_slug) === FALSE) break;
            $url = str_replace('/' . $taxonomy_slug, '', $url);
            break;
    endswitch;
    return $url;
}
// Add our custom product cat rewrite rules
function devvn_product_category_rewrite_rules($flash = false) {
    $terms = get_terms( array(
        'taxonomy' => 'product_cat',
        'post_type' => 'product',
        'hide_empty' => false,
    ));
    if($terms && !is_wp_error($terms)){
        $siteurl = esc_url(home_url('/'));
        foreach ($terms as $term){
            $term_slug = $term->slug;
            $baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
            add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top');
            add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top');
            add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top');
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_product_category_rewrite_rules');

/*Sửa lỗi khi tạo mới taxomony bị 404*/
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 );
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) {
    devvn_product_category_rewrite_rules(true);
}

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button