Last active
October 14, 2024 15:00
-
-
Save levantoan/fc705c5ae4739e6d87e2ec51b257ea5c to your computer and use it in GitHub Desktop.
How to set product category base the same as shop base in WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Support WPML - 25/02/2019 | |
/* | |
function devvn_product_category_base_same_shop_base( $flash = false ){ | |
global $sitepress; | |
$languages = icl_get_languages('skip_missing=0&orderby=code'); | |
if($languages && !empty($languages)){ | |
$original_lang = ICL_LANGUAGE_CODE; | |
foreach($languages as $key=>$lang) { | |
$new_lang = $key; | |
$sitepress->switch_lang($new_lang); | |
$terms = get_terms(array( | |
'taxonomy' => 'product_cat', | |
'post_type' => 'product', | |
'hide_empty' => false, | |
)); | |
if ($terms && !is_wp_error($terms)) { | |
$siteurl = apply_filters( 'wpml_home_url', get_home_url('/')); | |
$siteurl = ($sitepress->get_default_language() == $key) ? $siteurl.'/' : $siteurl; | |
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'); | |
} | |
} | |
$sitepress->switch_lang($original_lang); | |
} | |
} | |
if ($flash == true) | |
flush_rewrite_rules(false); | |
} | |
add_filter( 'init', 'devvn_product_category_base_same_shop_base'); | |
/*Sửa lỗi khi tạo mới taxomony bị 404*/ | |
add_action( 'create_term', 'devvn_product_cat_same_shop_edit_success', 10, 2 ); | |
function devvn_product_cat_same_shop_edit_success( $term_id, $taxonomy ) { | |
devvn_product_category_base_same_shop_base(true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
***** Into: | |
First, Go to Setting / Permalinks: | |
Shop base: shop | |
Product category base: shop (same as shop base) | |
Product permalink base: Shop base with category, e.g. shop/%product_cat% | |
***** Then, We have | |
Shop page: http://domain.com/shop/ | |
Category page: http://domain.com/shop/computer | |
Product page: http://domain.com/shop/computer/dell-vostro-5459 | |
***** But It 404 error at "Category page" | |
***** Don't worry, I fixed it by code below. Insert it into functions.php in your theme. | |
***** Post link | |
http://levantoan.com/cach-cai-dat-base-cua-danh-muc-san-pham-giong-voi-base-cua-trang-san-pham/ | |
*/ | |
//base product category same base shop Page for woocommerce | |
function devvn_product_category_base_same_shop_base( $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_filter( 'init', 'devvn_product_category_base_same_shop_base'); | |
/*Sửa lỗi khi tạo mới taxomony bị 404*/ | |
add_action( 'create_term', 'devvn_product_cat_same_shop_edit_success', 10, 2 ); | |
function devvn_product_cat_same_shop_edit_success( $term_id, $taxonomy ) { | |
devvn_product_category_base_same_shop_base(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sure this can be of help to many people, if you are old school and don't want the classic final trailing slash at the end of WordPress URL, you need the following rewrites:
This also adds code for subcategories, for those interested. Comment or uncomment as needed.