Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active August 10, 2021 04:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levantoan/a10876ceaee4324cdeb6ae18af11487a to your computer and use it in GitHub Desktop.
Save levantoan/a10876ceaee4324cdeb6ae18af11487a to your computer and use it in GitHub Desktop.
Xóa bỏ product trong đường dẫn của sản phẩm Xem chi tiết tại http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/
<?php
add_filter( 'term_link', 'plugins_remove_category_from_url', 10, 3);
function plugins_remove_category_from_url($termlink, $term, $taxonomy){
if($taxonomy == 'product_cat'){
$termlink = str_replace('/'._x( 'product-category', 'slug', 'woocommerce' ).'/', '/', $termlink);
}
return $termlink;
}
add_action('init', 'devvn_product_category_rewrite_rules');
function devvn_product_category_rewrite_rules($flash = false)
{
add_rewrite_rule('(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=$matches[1]&feed=$matches[2]', 'bottom');
add_rewrite_rule('(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=$matches[1]&feed=$matches[2]', 'bottom');
add_rewrite_rule('(.+?)/embed/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'bottom');
add_rewrite_rule('(.+?)/page/?([0-9]{1,})/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'bottom');
add_rewrite_rule('(.+?)/?$', 'index.php?product_cat=$matches[1]', 'bottom');
if($flash == true){
flush_rewrite_rules(false);
}
}
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 );
add_action( 'edit_term', 'devvn_new_product_cat_edit_success' );
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) {
devvn_product_category_rewrite_rules(true);
}
if(!is_admin()){
add_filter('request', 'taxonomy_change_term_request', 1, 1 );
function taxonomy_change_term_request($query){
global $wp_rewrite;
/*$rewrite = $wp_rewrite->wp_rewrite_rules();
echo '<pre>';
print_r($rewrite);
echo '</pre>';*/
if(isset($query)){
if(isset($query['pagename'])) {
$name = $query['pagename'];
$term = get_term_by('slug', $name, 'product_cat');
if (isset($name) && $term && !is_wp_error($term)) {
unset($query['pagename']);
$query['product_cat'] = $name;
}
return $query;
};
if(isset($query['name'])) {
$name = $query['name'];
$term = get_term_by('slug', $name, 'product_cat');
if (isset($name) && $term && !is_wp_error($term)) {
unset($query['name']);
$query['product_cat'] = $name;
}
return $query;
};
if(isset($query['attachment'])):
$name = $query['attachment'];
$term = get_term_by('slug', $name, 'product_cat');
if (isset($name) && $term && !is_wp_error($term)) {
unset($query['attachment']);
$query['product_cat'] = $name;
}
endif;
return $query;
}
}
}
add_action('template_redirect', 'plugins_product_cat_old_term_redirect');
function plugins_product_cat_old_term_redirect() {
$taxonomy_name = 'product_cat';
$taxonomy_slug = _x( 'product-category', 'slug', 'woocommerce' );
if (is_tax($taxonomy_name) && strpos($_SERVER['REQUEST_URI'], $taxonomy_slug) ) :
wp_redirect(site_url(str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI'])), 301);
exit();
endif;
}
<?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
add_filter('rewrite_rules_array', 'devvn_product_category_rewrite_rules');
function devvn_product_category_rewrite_rules($rules) {
$new_rules = array();
$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'));
$new_rules[$baseterm.'?$'] = 'index.php?product_cat='.$term_slug;
$new_rules[$baseterm.'page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]';
$new_rules[$baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]';
}
}
return $new_rules + $rules;
}
<?php
/*
* Remove product form url
* Chú ý Thay product bằng slug hiện tại của bạn
* Link hướng dẫn cụ thể: http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/
ERROR CODE
DO NOT USE THIS CODE
*/
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( '/product/', '/', $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' );
<?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);
}
<?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');
@phongtran89
Copy link

phongtran89 commented Mar 17, 2017

Cho mình hỏi cách setup để chạy được sao bạn? Mình copy bỏ vào hàm function mà vẫn không sử dụng được?

mình là newber

@levantoan
Copy link
Author

@phongtran89 sau khi copy vào functions.php bạn phải update lại permalink thì nó mới áp dụng cái vừa thay đổi nhé. Link cụ thể đây bạn http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/

@phongtran89
Copy link

Mình thấy trong bài này bạn sử dụng code để remove product-category . Nhưng trong link http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/ bạn lại sử dụng plugin? Vậy mình sử dụng code với plugin chung luôn hay chỉ chọn 1 trong 2 thôi???? @levantoan

@levantoan
Copy link
Author

@phongtran89 ah cái code kia chưa dc tối ưu lên mình chuyển sang dùng plugin. Cái chỗ này lưu code chơi vậy. sao bạn lại search vào đây ta :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment