Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same base url for categories and products in woocommerce

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

like image 953
limilo Avatar asked Feb 24 '19 16:02

limilo


People also ask

How do I get a product category link in WooCommerce?

$link = get_term_link( $product_cat_id, 'product_cat' ); To get the url of the product category.

Can a product have multiple categories in WooCommerce?

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".


1 Answers

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');
like image 196
p_e_88 Avatar answered Sep 30 '22 22:09

p_e_88