Hướng dẫn xóa san-pham , danh-muc-san-pham trong đường dẫn sản phẩm Woocommerce

26 Tháng Sáu, 2024
(1 đánh giá)

Với mặc định của Woocommerce, chi tiết sản phẩm sẽ có dạng domain/san-pham/ten-san-pham và danh mục sản phẩm sẽ ở dạng domain/danh-muc-san-pham/ten-danh-muc. Một phần đường dẫn như vậy sẽ làm xấu và dài url, còn ảnh hưởng tới SEO thì không phải là trực tiếp. Tùy vào nhu cầu của người sử dụng, mình sẽ hướng dẫn người dùng bỏ 2 đoạn trên đi.

Loại bỏ /san-pham/ ra khỏi đường dẫn chi tiết sản phẩm

Mặc định của Woocommerce sẽ là /san-pham/ nhưng đôi khi website bạn setup lại khác, để kiểm tra bạn vào:

Cài đặt -> Đường dẫn tĩnh
Cài đặt -> Đường dẫn tĩnh
Sửa đường dẫn thành /san-pham/
Sửa đường dẫn thành /san-pham/

Vậy là bạn xác định được đường dẫn url của chi tiết sản phẩm của bạn là gì, ở đây là san-pham.

Bạn copy đoạn ở đây vào file functions.php, nếu tùy biến đường dẫn của bạn không phải là san-pham, để dễ dàng bạn ctrl+f tìm san-pham thay thế bằng nội dung trong “Tùy biến đường dẫn” của bạn:

/*
* Code Bỏ /san-pham/ hoặc /cua-hang/ hoặc /shop/ hoặc /product/ ... có hỗ trợ dạng %product_cat%
* Thay /cua-hang/ bằng slug hiện tại của bạn
*/
function giniit_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 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', 'giniit_remove_slug', 10, 2 );
/*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/
function giniit_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', 'giniit_woo_product_rewrite_rules');
/*Fix lỗi khi tạo sản phẩm mới bị 404*/
function giniit_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) {
            giniit_woo_product_rewrite_rules(true);
        }
    }
}
add_action('wp_insert_post', 'giniit_woo_new_product_post_save');

Note: Sau khi bạn cập nhật file functions.php, hãy vào Cài đặt -> Đường dẫn tĩnh ấn lưu thay đổi dể update url nhé. Nếu không bạn sẽ bị 404.

Ấn vào lưu thay đổi
Ấn vào lưu thay đổi

Loại bỏ /danh-muc-san-pham/ ra khỏi đường dẫn danh mục sản phẩm

Tương tự chi tiết sản phẩm, các bạn cần xác định của các bạn có phải /danh-muc-san-pham/ hay không ở “Đường dẫn tĩnh”:

Cài đặt -> Đường dẫn tĩnh
Cài đặt -> Đường dẫn tĩnh
Thay thành /danh-muc-san-pham/
Thay thành /danh-muc-san-pham/

Bạn copy đoạn dưới đây vào functions.php là được, tương tự nếu không phải /danh-muc-san-pham/ thì các bạn cứ ctrl+f tìm và thay thế nhé:

/*
* Loại bỏ /danh-muc-san-pham/ ở đường dẫn
* Thay /danh-muc-san-pham/ slug hiện tại của bạn. Mặc định là /danh-muc-san-pham/
*/
add_filter( 'term_link', 'giniit_product_cat_permalink', 10, 3 );
function giniit_product_cat_permalink( $url, $term, $taxonomy ){
    switch ($taxonomy):
        case 'product_cat':
            $taxonomy_slug = 'danh-muc-san-pham'; //Thay bằng slug hiện tại của bạn. Mặc định là /danh-muc-san-pham/
            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 giniit_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', 'giniit_product_category_rewrite_rules');
/*Sửa lỗi khi tạo mới taxomony bị 404*/
add_action( 'create_term', 'giniit_new_product_cat_edit_success', 10, 2 );
function giniit_new_product_cat_edit_success( $term_id, $taxonomy ) {
    giniit_product_category_rewrite_rules(true);
}

Note: Sau khi bạn cập nhật file functions.php, hãy vào Cài đặt -> Đường dẫn tĩnh ấn lưu thay đổi dể update url nhé. Nếu không bạn sẽ bị 404.

guest
0 Góp ý
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận
Tin tức liên quan
Hitbox và Hurtbox trong Unity3D
Phàm từ xưa đến nay, nhiều chuyện nghĩ thì đơn giản nhưng lúc bắt tay vào làm thì gặp rất nhiều vấn đề đau đầu. Ví dụ khi một...
Command Pattern và ứng dụng trong Unity – Phần 1
Trong lập trình game, việc tương tác, điều khiển của người chơi với game là rất quan trọng. Do vậy, command pattern được sử dụng rộng rãi và phổ...
Các hàm cơ bản của Monobehaviour Unity: Update, LateUpdate, FixedUpdate
Với nhiều bạn mới làm quen với Unity, thì việc khi nào dùng Update, LateUpdate, FixedUpdate thường là khá mơ hồ. Thêm vào đó, ba hàm này ảnh hưởng...