is it possible to have the same base url for categories and products in woocommerce? At the moment it's giving my category page an 404 but products working fine.
What i'm trying to do is that both have the base "shop" Categories: demo.com/shop/category/ Products: demo.com/shop/category/product-name
$link = get_term_link( $product_cat_id, 'product_cat' ); To get the url of the product category.
There is a woocommerce shop, categories on the left side (sidebar) and on the right side products. A product can have multiple categories; for example, the product "Burger" is in the categories "Food" and "Fastfood".
The solution could be this one Permalinks for Woocommerce
Try to add this in your functions.php
function wpse_291143_generate_taxonomy_rewrite_rules( $wp_rewrite ) {
global $wp_rewrite;
$base = "shop";
$rules = array();
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ));
foreach($terms as $term) {
$term_children = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => intval($term->term_id),
'hide_empty' => false
)
);
if($term_children) {
foreach ( $term_children as $term_child ) {
$rules[$base . '/' . $term->slug . '/' . $term_child->slug . '/?$'] = 'index.php?product_cat=' . $term_child->slug;
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'wpse_291143_generate_taxonomy_rewrite_rules');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With