-
-
Save levantoan/fc705c5ae4739e6d87e2ec51b257ea5c to your computer and use it in GitHub Desktop.
<?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); | |
} |
<?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); | |
} |
For anyone still struggling to get pagination working, if you have a line that looks like this:
add_rewrite_rule($baseterm . '/page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]','top');
Change it to:
add_rewrite_rule($baseterm . 'page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]','top');
This made it work for me.
Came across this issue by chance. If you enter the wrong category, it still works instead of 404'ing like it should
For example /shop/summer/hats/
also works as
/shop/summer-time/hats/
After running this on a production shop for about a year I would recommend against it. I had a lot of weird 404 errors, broken styling and performance issues that all seemed to have gone away when we got rid of this. Some people seem to have had better luck but just wanted to share my perspective, could be something with our hosting that didn't play right.
I've had no issues running this at all apart from the categories for one site reporting "Alternate page with proper canonical tag" which led me to discover the issue.
I'd just like for this implementation to be a bit stricter on URL's. Other implementations of this also have the same issue.
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:
// Add rewrite rules for the category base term without trailing slash
add_rewrite_rule($baseterm . '?$', 'index.php?product_cat=' . $term_slug, 'top');
// Add rewrite rules for the category base term with trailing slash
//add_rewrite_rule($baseterm . '/?$', 'index.php?product_cat=' . $term_slug, 'top');
// Add rewrite rules for category pagination without trailing slash
add_rewrite_rule($baseterm . 'page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]', 'top');
// Add rewrite rules for category pagination with trailing slash
//add_rewrite_rule($baseterm . '/page/([0-9]{1,})/?$', 'index.php?product_cat=' . $term_slug . '&paged=$matches[1]', 'top');
// Handle nested categories (subcategories) for pagination without trailing slash
add_rewrite_rule($baseterm . '(.+?)/page/([0-9]{1,})/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'top');
// Handle nested categories (subcategories) for pagination with trailing slash
//add_rewrite_rule($baseterm . '/(.+?)/page/([0-9]{1,})/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'top');
This also adds code for subcategories, for those interested. Comment or uncomment as needed.
After doing some more testing, I also started having issues with 404s with the code above. I found this that, fingers crossed, seems to work better, both languages and pagination work for me: https://wordpress.org/support/topic/seo-url-for-category-and-product-pages/#post-12571113