Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - change product-category slug to shop base

Now I have tried figuring this out for two days and close to loosing my mind.

Now my links is mydomain.com/product-category/clothing/t-shirts/ and I want it to be mydomain.com/store/clothing/t-shirts/

I need to change my product-catogory permalinks. but when i change it to /store/ in Setting - Permalinks - Product category base i get a 404 when trying to access the product page.

Would really appreciate som help ! Thanks

like image 670
davr Avatar asked Jan 29 '15 18:01

davr


2 Answers

See my code here : http://levantoan.com/cach-cai-dat-base-cua-danh-muc-san-pham-giong-voi-base-cua-trang-san-pham/

Or in github https://gist.github.com/levantoan/fc705c5ae4739e6d87e2ec51b257ea5c

First, Go to Setting / Permalinks:

Shop base: store
Product category base: store (same as shop base)
Product permalink base: Shop base with category, e.g. /store/%product_cat%

Then insert code in functions.php

//base product category same base shop Page for woocommerce
add_filter( 'rewrite_rules_array', function( $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;
} );
like image 107
Toản Lê Văn Avatar answered Oct 16 '22 08:10

Toản Lê Văn


Solution with Polylang supporting.

According to @Toản Lê Văn.

add_filter( 'rewrite_rules_array', 'your_prefix_rewrite_rules', 9999 );

function your_prefix_rewrite_rules( $rules ) {
    $new_rules = [];

    $languages = pll_languages_list();
    $your_base = 'shop';

    foreach ( $languages as $lang ) {
        $terms = get_terms(
            [
                'taxonomy'   => 'product_cat',
                'post_type'  => 'product',
                'hide_empty' => false,
                'lang'       => $lang,
            ]
        );

        if ( empty( $terms ) || is_wp_error( $terms ) ) {
            continue;
        }

        $site_url = esc_url( home_url( '/' ) );
        foreach ( $terms as $term ) {
            $term_slug = $term->slug;
            $base_term = str_replace( $site_url, '', get_term_link( $term->term_id, 'product_cat' ) );

            $new_rules[ $base_term . '?$' ]                                    = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug;
            $new_rules[ $base_term . 'page/([0-9]{1,})/?$' ]                   = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&paged=$matches[1]';
            $new_rules[ $base_term . '(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?lang=' . $lang . '&product_cat=' . $term_slug . '&feed=$matches[1]';

        }

        if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] ) ) {
            unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/?$' ] );
        }

        if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] ) ) {
            unset( $rules[ '(' . $lang . ')/' . $your_base . '/(.+?)/([^/]+)?(:/([0-9]+))?/?$' ] );
        }

        if ( isset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] ) ) {
            unset( $rules[ '(' . $lang . ')/' . $your_base . '/.+?/[^/]+/([^/]+)/?$' ] );
        }
    }

    return $new_rules + $rules;
}
like image 1
Andrii Kovalenko Avatar answered Oct 16 '22 07:10

Andrii Kovalenko